【问题标题】:Table View Cells "jump" when going back from View Controller to Table View Controller从视图控制器返回到表视图控制器时,表视图单元“跳跃”
【发布时间】:2014-11-15 17:33:13
【问题描述】:

我有一个TableViewController,单击自定义单元格会将您带到相关的WebViewController

我遇到的问题是,当我点击WebViewController 中的“返回”时,TableViewController 中的表格视图单元格“跳转”。

一旦表格视图开始滚动,它们都会跳回正确的高度。所以我假设它与TableViewController 自定义单元格的高度有关,但我并不完全确定。

TableViewController.m

试过了:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[Two self]]) {
        return 490; // As of 11/13/14
    } else { // 2 other custom cells
        return tableView.rowHeight; // return the default height
    }
}

也试过了:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[One self]]) {
    ListTableViewCell *cellOne = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
    CGFloat heightOne = [cellOne.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightOne + 2;

    } else if ([model isKindOfClass:[Two self]]) {
    ListTableViewCellTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];
    CGFloat heightTwo = [cellTwo.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightTwo + 400;

    } else if ([model isKindOfClass:[Three self]]) {
    ListTableViewCellThree *cellThree = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];
    CGFloat heightThree = [cellThree.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightThree + 2;

    } else {
        return 300;
    }
}

更新:

也试过[cell setNeedsUpdateConstraints];[cell setNeedsLayout];

还尝试设置estimatedHeight多个不同的点,并同时添加Automatic Row Dimension

我们将不胜感激!将发布任何需要的额外信息。谢谢!

我只支持 iOS 8。我正在使用自动布局和情节提要。

自定义单元 2 的附加故事板信息:

故事板约束:

【问题讨论】:

  • 只是为了确保保持返回 490;在高度方法中并删除其他任何内容。你有什么?
  • 显示 WebViewController 后是否修改了 self.Model ?
  • @hasan83 抱歉,我不确定我是否理解您的句子。您是说要删除除返回 490 之外的所有内容;?谢谢!
  • @gottlieb no self.Model 在显示 WebViewController 后根本没有修改。有什么想法吗?
  • 如何添加 WebViewController ?使用 addSubview、presentViewController 还是将其推入导航堆栈?

标签: ios objective-c uitableview ios8 autolayout


【解决方案1】:

我认为您没有正确设置约束。我之前遇到过类似的问题,通过将Leading,Trailing,Top和Bottom间距的约束设置为UILabel来解决。

玩转约束。行为将开始改变。继续前进,直到你得到你想要的。

这似乎不是专业人士的技术回答。但是,它是如何通过苹果没有正确引入的那些虚拟约束来解决的。

另外,在返回 cellForRowAtIndexPath 中的单元格之前尝试 [cell setNeedsUpdateConstraints];。 还有:[cell setNeedsLayout];

【讨论】:

  • 感谢您的评论,我刚刚添加了我的约束的屏幕截图以提供帮助。我一直在玩这些约束 很多时候我认为我可能将它们设置错了,但即使我猜故事板中没有错误或警告,我也无法完全正确地得到约束。我认为它们是完美的,所以这就是为什么我现在正在查看我的代码,看看我是否遗漏了一些东西。让我知道您是否在我的限制中看到任何对您来说很突出的东西?谢谢!
  • 对于标签,只保留与容器的约束。使用图像视图删除那些。告诉我新的行为是什么
  • 如果我有静态值,它不会跳转,但是当我不使用静态值时它会跳转。
【解决方案2】:

我终于解决了这个问题,关键是计算一个估计的高度。似乎您返回的估计越准确,跳动/波动就越少。

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
     switch (yourTypeOfCell) {
         case something:
            return estimatedHeight;
            break;
         case default:
            return defaultValue;
            break;
     }
}

【讨论】:

    【解决方案3】:

    尝试在 viewDidLoad 中为您的 UITableView 设置 estimatedRowHeight 以帮助 UIKit 猜测您的 tableView 的行高

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tableView.estimatedRowHeight = 490.0;
    }
    

    【讨论】:

    • 我试过了,但后来把它注释掉了,因为它并没有改变表格视图单元格“跳跃”的行为。还有其他想法吗?谢谢!
    猜你喜欢
    • 2014-06-11
    • 1970-01-01
    • 2013-01-08
    • 2023-03-29
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    相关资源
    最近更新 更多