这里有一个挑战给大家...
我在我的UIViewController
中有一个正确加载的UICollectionView
。我也有一个自定义的UICollectionViewCell
类,其中包含一个UIButton
。
我从服务器检索到一个NSArray
,其中包含一些UIImage
对象,以便将一个背景图像分配给我的自定义UICollectionViewCell
的按钮。
这是我cellForItemAtIndexPath
函数的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
UserPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"userPhotoCell" forIndexPath:indexPath];
if (indexPath.section == 0) {
[[cell imageButton] setBackgroundImage:[userPublicImages objectAtIndex:indexPath.row] forState:UIControlStateNormal];
} else {
[[cell imageButton] setBackgroundImage:[userPrivateImages objectAtIndex:indexPath.row] forState:UIControlStateNormal];
}
return cell;
}
如您所见,这非常简单。
下面涉及到一种奇怪的行为:如果我将所有自定义的UICollectionViewCell
放在UICollectionView
的一个部分中,性能还可以接受...
有什么想法吗?
一些额外的信息: UICollectionView
有标题。 自定义标题。 目前只是一个带有UILabel
的UIView
。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil;
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHeader" forIndexPath:indexPath];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [headerView frame].size.width, 40.0f)];
if (indexPath.section == 0) {
[titleLabel setText:NSLocalizedStringFromTable (@"collectionTitle_publicPhotos", [[NSLocale preferredLanguages] objectAtIndex:0] , @"")];
} else {
[titleLabel setText:NSLocalizedStringFromTable (@"collectionTitle_privatePhotos", [[NSLocale preferredLanguages] objectAtIndex:0] , @"")];
}
[headerView addSubview:titleLabel];
reusableView = headerView;
}
return reusableView;
}