2016-04-11 13 views
1

私はiOS開発で新しく、まだ学習しています。簡単なテキストでもUITableViewのスクロールが遅くなります

テキストデータはunicodeにあり、各文字列はNSArray要素に保存されています。私はすでにheightForRowAtIndexPathを計算し、別の配列に別々に保存して、後で計算を最小限に抑えるようにしました。各文字列には、複数の行が1つの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方法最後に、また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; } 

示されています。

また、コアデータを使用するとスクロールのパフォーマンスが向上する可能性があるかどうかをご案内します。私は提案のために開いています。

答えて

2

セルの高さを別々に計算して配列に格納する必要はありません。 heightForRowAtIndexPathメソッドで各セルの高さを計算するだけで、computeHeightメソッドを完全に取り除くことができます。

この他にも、dequeueReusableCellWithIdentifierの中にcomputeHeightの方法が必要であることがわかりました。セルは、cellForRowAtIndexPathメソッドに実装されている必要があります。

のでbottomline:

1)はcomputeHeight方法

2)heightForRowAtIndexPathメソッドにセルの高さを計算するための移動コードを取り除きます。すべての行に対して正しい高さが返されていることを確認します。

3)現在のセルのインスタンスを追跡する必要がないので、self.customCell.を取り除く(self.customCell.customCellLabel.attributedText)cellForRowAtIndexPathメソッドに設定します。

+0

heightForRowメソッドで高さを計算してcomputeHeightメソッドを削除しても、スクロール中に滑らかさは維持されます。 computeHeightは、セルを描画する必要があるたびに長時間の計算を保存するためにのみ使用され、格納された配列から値を返すのは簡単です。この問題を考慮していただきありがとうございます。 – Manpreet

+0

** self.customCell **を使用して関数の高さを計算しています。代わりに** sizeWithFont:constrainedToSize:lineBreakMode **を使用して、動的な高さを計算してみてください。これを使用しても、計算が集中することはありません。また、テーブルの全セルの高さを予め計算するのではなく、セルの高さを計算するのが通例である。 – Pankaj

+0

オハイオ州私は提案を試み、それが動作するかどうかを見てください。 – Manpreet

関連する問題