【问题标题】:Conditionally show cells in a static cell tableview有条件地在静态单元格表​​格视图中显示单元格
【发布时间】:2014-11-06 03:11:37
【问题描述】:

我在界面生成器中设置了一个静态表格视图,但在某些情况下我不希望显示某些单元格。使用静态单元格完成此任务的最佳方法是什么?我真的很想使用静态单元格,因为 a)它有意义,因为没有数据源,b)我正在使用 IBDesignable/IBInspectable 渲染我的静态视图,并希望保持这种状态。

【问题讨论】:

    标签: ios xcode uitableview swift


    【解决方案1】:

    您应该能够添加 UITableViewDelegate 方法 tableView:heightForRowAtIndexPath: 并为您不想显示的任何行返回 0。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGFloat rowHeight = 44.0;
        if (indexPath.row == /* a row you want to hide */) {
            rowHeight = 0.0;
        }
        return rowHeight;
    }
    

    【讨论】:

    • 我曾想过这样做,但有一些原因我选择不这样做:一方面,我有交替的背景颜色;在 willDisplay 我根据 indexPath.row % 2 设置背景颜色 - 隐藏行而不删除它们会破坏此功能。此外,我在这些行中有文本字段,并链接了第一响应者 - 如果从技术上讲这些行仍然存在,它将导致隐藏的文本字段成为第一响应者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    相关资源
    最近更新 更多