【问题标题】:update UITableViewCell height in iOS7在 iOS7 中更新 UITableViewCell 高度
【发布时间】:2015-03-31 14:39:50
【问题描述】:

我正在尝试创建一个UITableViewCell,当滚动离开时,它会折叠成预览。

我设法做到了,使用布尔值 - 即 - 在 cellForRowAtIndexPath 中,我更改了当前单元格之前所有单元格的布尔数组。

在 iOS8 中它可以完美运行 - 问题是在 iOS7 中 heightForRowAtIndexPath 仅在 tableView 加载时被调用。

我知道我能做到:

[tableView beginUpdates];
[tableView endUpdates];

但我无法在 cellForRowAtIndexPath 中执行此操作(它崩溃了)

我可以在哪里运行它?

代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[collapsedChapters objectAtIndex:indexPath.row] boolValue] == YES)
    {
        return 73;
    }
    //else - calculate
    return 200; //the number is actually calculated - but simplifying
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =[self generateChapterCell:tableView indexPath:indexPath];
        [self handleLastVisible];
        return cell;
}

- (void)handleLastVisible
{

    NSInteger lastVisibleChapter = [self getLastVisibleRowOfSection:self.tableView section:chaptersSectionNum];
    if (lastVisibleChapter > lastViewedChapter) //should work for -1 and 9999 as well
    {
        lastViewedChapter = lastVisibleChapter;
    }

    NSInteger firstVisibleChapter = [self getFirstVisibleRowOfSection:self.tableView section:chaptersSectionNum];
    if (firstVisibleChapter == -1)
    {
        //we are before - don't touch
        return;
    }

    NSNumber* yesObj = [NSNumber numberWithBool:YES];
    //in case of 9999 - we will stop at the end of the for loop - it's OK
    for (int i=0; i < [collapsedChapters count]; i++)
    {
        if (i == firstVisibleChapter)
        {
            break;
        }
        if ([[collapsedChapters objectAtIndex:i] boolValue] == NO)
        {
            [chapterFirstCollapse replaceObjectAtIndex:i withObject:yesObj];
        }
        [collapsedChapters replaceObjectAtIndex:i withObject:yesObj];
    }
}

【问题讨论】:

  • 实际上每次显示单元格时都应该调用heightForRowAtIndexPath。如果它在 iOS7 上不起作用,那你一定是做错了什么

标签: ios uitableview ios7


【解决方案1】:

我注意到heightForRowAtIndexPath: 调用[[collapsedChapters objectAtIndex:indexPath.row] boolValue] == YES。我假设这意味着布尔值是一个切换,表示特定单元格是否“扩展”,对吧?

如果是这样,几年前我也遇到过同样的问题。与其为整个表设置一个部分,然后每个“章节”有一行,不如每个“章节”有一个 部分,然后根据“章节”是否为每个部分更改每个部分的行数展开或折叠 - 折叠 1 行,展开 2 行。对于扩展的“章节”,您将扩展信息放在该部分的第二行。

通过这种方式展开章节,您可以使用 insertRowsAtIndexPaths: 并折叠,removeRowsAtIndexPaths:,一切都会正常运行,包括用户期望的所有漂亮的表格视图动画。

【讨论】:

  • 是的......但在我的情况下,单元格是不同的(不仅仅是添加信息),所以这个技巧不起作用......
  • 好吧 - 这是几年前的事了,但我确实记得在尝试这样做时感觉 API 真的在与我作斗争。你确定你不能重新设计它,所以额外的信息只会从底部弹出吗?
猜你喜欢
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多