首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式创建UICollectionReusableView重叠问题

以编程方式创建UICollectionReusableView重叠问题
EN

Stack Overflow用户
提问于 2015-07-03 18:05:49
回答 1查看 1.2K关注 0票数 0

因此,-addSubview似乎两次将UILabel添加到UICollectionReusableView

代码语言:javascript
复制
- (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;
}

以下代码不起作用:

代码语言:javascript
复制
if (reusableview==nil) {
   reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}

因此,可重用视图永远不会是nill。

EN

回答 1

Stack Overflow用户

发布于 2015-07-03 19:31:14

如果您想要自定义一个UICollectionReusableView,您需要像对一个UICollectionViewCell一样对它进行子类化。否则,你基本上就是在每次重用你的reusableview时添加一个标签(在scroll上,或者在ex中使用reloadData )。

我之前写了一篇关于这个的帖子:UICollectionView adding image to a cell

只要对你的reusableview做同样的事情,让我知道它是否解决了你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31204075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档