【问题标题】:How to get the preset height that is set in the storyboard for tableView static Cell?如何获取在情节提要中为 tableView 静态单元格设置的预设高度?
【发布时间】:2012-08-24 08:21:11
【问题描述】:

试图隐藏和自定义静态单元格的高度。我知道这可能不是最好的方法。如果有人知道更好的方法,请指教。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (![Mode isEqualToString:@"HIDE"]) {
        if (indexPath.row == 2) {
            return 0.0;
        }
    }

    return "DEFAUlT_HEIGHT";

}

如何从情节提要中获取默认高度?故事板中每个单元格的高度都不同。反正有更好的定制吗?提前致谢。

【问题讨论】:

    标签: objective-c ios uitableview


    【解决方案1】:

    看看这个帖子:Hide static cells

    它谈到了以编程方式隐藏静态单元格。这是公认的答案:

    1.隐藏单元格

    没有办法直接隐藏单元格。 UITableViewController 是 提供静态单元格的数据源,当前存在 没有办法告诉它“不提供单元格 x”。所以我们必须提供我们的 自己的数据源,按顺序委托给 UITableViewController 获取静态单元格。

    最简单的方法是继承 UITableViewController,并覆盖所有方法 隐藏单元格时需要采取不同的行为。

    在最简单的情况下(单节表,所有单元格具有相同的 高度),这将是这样的:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
        return [super tableView:tableView numberOfRowsInSection:section] - numberOfCellsHidden; }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // Recalculate indexPath based on hidden cells
        indexPath = [self offsetIndexPath:indexPath];
    
        return [super tableView:tableView cellForRowAtIndexPath:indexPath]; }
    
    - (NSIndexPath*)offsetIndexPath:(NSIndexPath*)indexPath {
        int offsetSection = indexPath.section; // Also offset section if you intend to hide whole sections
        int numberOfCellsHiddenAbove = ... // Calculate how many cells are hidden above the given indexPath.row
        int offsetRow = indexPath.row + numberOfCellsHiddenAbove;
    
        return [NSIndexPathindexPathForRow:offsetRow inSection:offsetSection]; }
    

    如果您的表格有多个部分,或者 单元格具有不同的高度,您需要覆盖更多方法。 同样的原则在这里适用:您需要偏移 indexPath, section 并在委派给超级之前行。

    还要记住,方法的 indexPath 参数 didSelectRowAtIndexPath: 对于同一个单元格会有所不同, 取决于状态(即隐藏的单元格数量)。所以它是 总是抵消任何 indexPath 参数和工作可能是个好主意 使用这些值。

    2。动画变化

    正如 Gareth 已经说过的,如果您制作动画,则会出现重大故障 使用 reloadSections:withRowAnimation: 方法进行更改。

    我发现如果你调用 reloadData: 紧接着, 动画得到了很大改善(只剩下小故障)。该表是 动画后正确显示。

    所以我正在做的是:

    - (void)changeState {
         // Change state so cells are hidden/unhidden
         ...
    
        // Reload all sections
        NSIndexSet* reloadSet = [NSIndexSetindexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:tableView])];
    
        [tableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [tableView reloadData]; }
    

    如果这有帮助,请去那里投票 henning77 的答案。

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 1970-01-01
      • 2016-08-02
      • 2015-06-04
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多