【问题标题】:Update not visible cells in UICollectionView更新 UICollectionView 中不可见的单元格
【发布时间】:2015-06-24 11:13:06
【问题描述】:

我的应用中需要一个可扩展的 UICollectionView - 所以我遇到了这个项目 (https://github.com/apploft/APLExpandableCollectionView)。它是 UICollectionView 的子类,实现了展开和折叠行为。

我已扩展演示应用程序以在可展开单元格中显示 + 或 - 按钮。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
APLCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"APLCollectionViewCell" forIndexPath:indexPath];

if (indexPath.item == 0) {
    cell.label.text = [NSString stringWithFormat:@"Section %li", (long)indexPath.section + 1];
    cell.backgroundColor = [UIColor colorWithRed:58./255. green:165./255. blue:192./255. alpha:1.];
    cell.indentView.hidden = YES;
    cell.label_OnOff.text = @"+";
} else {
    cell.label.text = [NSString stringWithFormat:@"Item %li", (long)indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:58./255. green:165./255. blue:192./255. alpha:.5];
    cell.indentView.hidden = NO;
    [cell.label_OnOff setHidden:YES];
}

return cell; 
}

为了在 + 和 - 之间切换,我实现了委托方法:

- (void)collectionView:(UICollectionView *)collectionView didCollapseItemAtIndexPath:(NSIndexPath *)indexPath
{
    APLCollectionViewCell *cell = (APLCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    cell.label_OnOff.text = @"+";
}

- (void)collectionView:(UICollectionView *)collectionView didExpandItemAtIndexPath:(NSIndexPath *)indexPath
{
    APLCollectionViewCell *cell = (APLCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    cell.label_OnOff.text = @"-";
}

正如您在屏幕截图中看到的,可见单元格已正确更新。一旦我向下滚动到以前的不可见单元格,+ 按钮就会消失。 当 UICollectionView 中只有几个项目时不会出现此问题,因此无需滚动到更多项目。

我是否对不可见细胞的 IndexPath 做错了什么,或者您对我有任何其他提示吗? 谢谢!

【问题讨论】:

    标签: ios uicollectionview uicollectionviewcell expand nsindexpath


    【解决方案1】:

    添加下面一行

    cell.indentView.hidden = NO;
    

    就在 cellForItemAtIndexPath 中的 if else 条件之前。它可能会帮助你。

    【讨论】:

      【解决方案2】:

      谢谢巴努,

      您的回答为我指明了正确的方向。集合视图中的单元格已被重用,因此我在检查 IndexPath 时必须设置 cell.label_OnOff.hidden = NO;cell.label_OnOff.hidden = YES;

      【讨论】:

      • 欢迎。如果你得到答案,请接受答案。享受:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2013-09-10
      • 1970-01-01
      相关资源
      最近更新 更多