【问题标题】:Create UICollectionReusableView programmatically overlapping issue以编程方式创建 UICollectionReusableView 重叠问题
【发布时间】:2015-07-03 10:05:48
【问题描述】:

看来-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)];
}

所以可重用视图永远不会是空的。

【问题讨论】:

    标签: uicollectionview uicollectionreusableview


    【解决方案1】:

    如果你想自定义一个 UICollectionReusableView,你需要像对 UICollectionViewCell 一样对它进行子类化。否则,您基本上是在每次重用可重用视图时添加一个标签(例如在滚动或 reloadData 上)。

    不久前我发了一个帖子:UICollectionView adding image to a cell

    只需对您的可重用视图执行相同的操作,如果它解决了您的问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      相关资源
      最近更新 更多