【发布时间】:2016-08-01 05:50:53
【问题描述】:
我是 iOS 开发的新手,还在学习。
我在unicode 中有文本数据,其中每个字符串都保存到NSArray 元素中。我已经计算了heightForRowAtIndexPath 并将其单独保存在另一个数组中,以便以后最小化计算。每个字符串在一个cell 中显示多行。
我创建了一个custom cell 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];
}
}
另外,下面显示了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