【问题标题】:UITableView scrolling lags even for simple text即使对于简单的文本,UITableView 滚动也会滞后
【发布时间】:2016-08-01 05:50:53
【问题描述】:

我是 iOS 开发的新手,还在学习。

我在unicode 中有文本数据,其中每个字符串都保存到NSArray 元素中。我已经计算了heightForRowAtIndexPath 并将其单独保存在另一个数组中,以便以后最小化计算。每个字符串在一个cell 中显示多行。

我创建了一个custom cell xib 并按要求注册。一切正常,但问题是当我滚动时,tableViewlaggy。稳定文本需要几毫秒,并且稳定出现故障,因此滚动不像所需的那样平滑,因为这种滞后是可见的。

这里是代码。

viewDidLoad 中调用此方法来计算行的高度。

 -(void) computeHeight 
{

    [array removeAllObjects];
    CGFloat height = 0;
    float high;

    self.customCell = [self.tableView dequeueReusableCellWithIdentifier:customCellIdentifier];

    for(int i = 0; i < self.gurbani.count; i++){
        self.customCell.customCellLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:self.gurbani[i] attributes:normalAttributes];
        [self.customCell.contentView layoutIfNeeded];
        height = [self.customCell.customCellLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

        high = height+20.0;

        [array insertObject:[NSNumber numberWithFloat:high] atIndex:i];
    }
}

另外,下面显示了cellForRowAtIndexPath 方法

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Fetch A Cell From Given TableView And Assign It To CustomCell
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:customCellIdentifier];

color = textColors[indexPath.row % 8];

// Customize And Feed The Cell Here
if(indexPath.row == 0 | indexPath.row == 1 | indexPath.row == 41 ) {
    cell.contentView.backgroundColor = color;
    cell.customCellLabel.backgroundColor = color;
    cell.customCellLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:self.gurbani[indexPath.row] attributes:titleAttributes];
    cell.customCellLabel.textColor = [UIColor whiteColor];

}
else {
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.customCellLabel.backgroundColor = [UIColor whiteColor];
    cell.customCellLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:self.gurbani[indexPath.row] attributes:normalAttributes];
    cell.customCellLabel.textColor = color;
}
// Return The Completed Cell Here
return cell; }

最后,在heightForRowAtIndexPath

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [array[indexPath.row] floatValue]; }

另外,我在单元格的 contentView 和 tableView 的其他部分中指定了不透明。

还指导我使用核心数据是否可能提高滚动性能。我愿意接受建议。

【问题讨论】:

    标签: ios objective-c iphone uitableview scroll


    【解决方案1】:

    您不需要单独计算单元格的高度并将其存储在数组中。您可以在 heightForRowAtIndexPath 方法中计算每个单元格的高度,并完全摆脱 computeHeight 方法。

    除此之外,我看到您在computeHeight 方法中使用了dequeueReusableCellWithIdentifier,这是不必要的。该单元格应以cellForRowAtIndexPath 方法填充。

    所以底线:

    1) 摆脱computeHeight 方法

    2) 将计算单元格高度的代码移至heightForRowAtIndexPath 方法。确保为每一行返回正确的高度。

    3) 不需要跟踪当前单元格的实例,所以去掉self.customCell.cellForRowAtIndexPath 方法中设置文本(self.customCell.customCellLabel.attributedText)

    【讨论】:

    • 即使我在 heightForRow 方法中计算高度并删除了 computeHeight 方法,滚动时仍然没有平滑度。每次需要绘制单元格时,我只使用 computeHeight 来节省冗长的计算,并且它很容易从存储的数组中返回一个值,而不是从头开始计算。并感谢您的建议和考虑这个问题
    • 您正在使用 self.customCell 来计算函数中的高度。尝试使用 sizeWithFont: constrainedToSize: lineBreakMode 来计算动态高度。使用它不会使计算密集。此外,习惯上计算显示单元格的高度,而不是预先计算表格中所有单元格的高度。
    • 好的,我会试试这个建议,看看是否可行。
    【解决方案2】:

    唯一的问题是 unicode 文本。这是旁遮普语的 unicode 文本。然后我为该字体使用了带有重写文本的自定义字体,问题得到了修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      相关资源
      最近更新 更多