ios - UITableView 设置为静态单元格。是否可以以编程方式隐藏某些单元格?

UITableView 设置为静态单元格。

是否可以通过编程方式隐藏某些单元格?

最佳答案

在 UITable 中隐藏静态单元格:

  1. 添加此方法:

在你的 UITableView Controller 委托(delegate)类中:

objective-C :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    if(cell == self.cellYouWantToHide)
        return 0; //set the hidden cell's height to 0

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

swift :

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    var cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)

    if cell == self.cellYouWantToHide {
        return 0
    }

    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

将为 UITable 中的每个单元格调用此方法。一旦它为您要隐藏的单元格调用它,我们将其高度设置为 0。我们通过为其创建一个导出来识别目标单元格:

  1. 在设计器中,为要隐藏的单元格创建一个导出。 一个这样的单元格的导出在上面称为“cellYouWantToHide”。
  2. 在 IB 中选中“剪辑 subview ”以查找要隐藏的单元格。 您要隐藏的单元格需要具有 ClipToBounds = YES。否则文字会堆积在 UITableView 中。

https://stackoverflow.com/questions/8260267/

相关文章:

iphone - iPhone 上 NSString 的 AES 加密

iphone - 如何获取 UIImage 并给它一个黑色边框?

objective-c - NSInteger 的 NSLog/printf 说明符?

objective-c - RootViewController 切换过渡动画

objective-c - 检测 NSString 是否包含...?

ios - SDK iOS 8.0 中的产品类型单元测试包需要代码签名

objective-c - 为什么在 iOS 中使用前导下划线重命名合成属性?

ios - 如何比较 UIColors?

iphone - 如何自动调整 UIScrollView 的大小以适合其内容

ios - 以编程方式触发按钮单击事件?