Ios 即使对于简单文本,UITableView滚动也会滞后

Ios 即使对于简单文本,UITableView滚动也会滞后,ios,objective-c,iphone,uitableview,scroll,Ios,Objective C,Iphone,Uitableview,Scroll,我是iOS开发新手,还在学习 我在unicode中有文本数据,其中每个字符串都保存到NSArray元素中。我已经计算了RowAtIndexPath的高度,并将其单独保存在另一个数组中,以尽量减少以后的计算。每个字符串都有多行显示在一个单元格中 我创建了一个自定义单元格xib,并根据需要注册。一切正常,但问题是当我滚动时,tableView是laggy。文本结算需要几毫秒的时间,结算时会出现闪烁,因此滚动不如所需的平滑,因为该延迟是可见的 这是代码 在viewDidLoad中调用此方法来计算行的高

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

我在
unicode
中有文本数据,其中每个字符串都保存到
NSArray
元素中。我已经计算了RowAtIndexPath的
高度,并将其单独保存在另一个数组中,以尽量减少以后的计算。每个字符串都有多行显示在一个
单元格中

我创建了一个
自定义单元格xib
,并根据需要注册。一切正常,但问题是当我滚动时,
tableView
laggy
。文本结算需要几毫秒的时间,结算时会出现闪烁,因此滚动不如所需的平滑,因为该延迟是可见的

这是代码

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];
    }
}
最后,在索引路径的高度中

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [array[indexPath.row] floatValue]; }
另外,我在单元格的contentView和
表视图的其他部分中指定了不透明


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

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

除此之外,我看到您正在使用
dequeueReusableCellWithIdentifier
内部
computeHeight
方法,这是不必要的。单元格应填充在
cellforrowatinexpath
方法中

所以底线是:

1) 摆脱
computeHeight
方法

-(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; }
2) 将用于计算单元格高度的代码移动到
heightforrowatinexpath
方法。确保为每行返回正确的高度


3) 无需跟踪当前单元格的实例,因此摆脱
self.customCell.
cellForRowAtIndexPath
方法中设置文本
(self.customCell.customCellLabel.AttributeText)

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