因此,-addSubview似乎两次将UILabel添加到UICollectionReusableView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
label.text=[NSString stringWithFormat:@"Recipe Group #%i", indexPath.section + 1];
[reusableview addSubview:label];
return reusableview;
}
return nil;
}以下代码不起作用:
if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}因此,可重用视图永远不会是nill。
发布于 2015-07-03 19:31:14
如果您想要自定义一个UICollectionReusableView,您需要像对一个UICollectionViewCell一样对它进行子类化。否则,你基本上就是在每次重用你的reusableview时添加一个标签(在scroll上,或者在ex中使用reloadData )。
我之前写了一篇关于这个的帖子:UICollectionView adding image to a cell
只要对你的reusableview做同样的事情,让我知道它是否解决了你的问题。
https://stackoverflow.com/questions/31204075
复制相似问题