私はiOS開発で新しく、まだ学習しています。簡単なテキストでもUITableViewのスクロールが遅くなります
テキストデータはunicode
にあり、各文字列はNSArray
要素に保存されています。私はすでにheightForRowAtIndexPath
を計算し、別の配列に別々に保存して、後で計算を最小限に抑えるようにしました。各文字列には、複数の行が1つの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
方法最後に、またheightForRowAtIndexPath
、
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [array[indexPath.row] floatValue]; }
で、iはセルのcontentViewとtableView
の他の部分に不透明指定した
-(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; }
示されています。
また、コアデータを使用するとスクロールのパフォーマンスが向上する可能性があるかどうかをご案内します。私は提案のために開いています。
heightForRowメソッドで高さを計算してcomputeHeightメソッドを削除しても、スクロール中に滑らかさは維持されます。 computeHeightは、セルを描画する必要があるたびに長時間の計算を保存するためにのみ使用され、格納された配列から値を返すのは簡単です。この問題を考慮していただきありがとうございます。 – Manpreet
** self.customCell **を使用して関数の高さを計算しています。代わりに** sizeWithFont:constrainedToSize:lineBreakMode **を使用して、動的な高さを計算してみてください。これを使用しても、計算が集中することはありません。また、テーブルの全セルの高さを予め計算するのではなく、セルの高さを計算するのが通例である。 – Pankaj
オハイオ州私は提案を試み、それが動作するかどうかを見てください。 – Manpreet