繁体   English   中英

表视图单元格从View Controller返回到Table View Controller时“跳转”

[英]Table View Cells “jump” when going back from View Controller to Table View Controller

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

我遇到的问题是,当我在WebViewController点击“Back”时, TableViewController的Table View Cells“跳转”。

在此输入图像描述

一旦表视图开始滚动,它们就会跳回到正确的高度。 所以我假设它与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的其他故事板信息: 在此输入图像描述在此输入图像描述

故事板约束:

在此输入图像描述

我相信你没有正确设置约束。 我之前遇到了类似的问题,并通过设置UILabel的前导,尾随,顶部和底部间距的约束来解决。

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

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

另外,试试[cell setNeedsUpdateConstraints]; 在cellForRowAtIndexPath中返回单元格之前。 并且: [cell setNeedsLayout];

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM