0

私のアプリでは、tableView に表示するコンテンツが約 1000 あります。1 ~ 3 行の UILabel があるため、各コンテンツの高さは異なります。現在、tableView デリゲート関数で各セルの高さを計算して返します。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

そして、その計算方法は次のとおりです。

contentCell = (RSContentViewCell *)self.tmpCell;
UIFont *font = contentCell.myTextLabel.font;
width = contentCell.myTextLabel.frame.size.width + 30;

size = [contentStr sizeWithFont:font
    constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
    lineBreakMode:contentCell.myTextLabel.lineBreakMode];

height = size.height;
return height;

動作しますが、それらの高さを計算するのに約 0.5 秒かかるため、計算プロセス中にアプリが応答しないため、UX はあまり良くありません。では、これらのセルの高さを計算する正しい方法と正しい場所はどこですか?

アップデート

データはサーバーからのもので、テーブルビューに入ったときに要求されます。

4

4 に答える 4

1

サーバーからデータをロードしているため、何があっても遅延があります。

=>テーブルをリロードする/スピナーなどを削除する前に、バックグラウンドで高さの計算を行うことをお勧めします.

// Method you call when your data is fetched
- (void)didReciveMyData:(NSArray *)dataArray {
    // start background job
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.myCachedHeightArray = [[NSMutableArray alloc] initWithCapacity:[dataArray count]];
        int i = 0;
        for (id data in dataArray) {
            float height;

            // do height calculation

            self.myCachedHeightArray[i] = [NSNumber numberWithFloat:height];// assign result to height results
            i++;
        }

        // reload your view on mainthread
        dispatch_async(dispatch_get_main_queue(), ^{
            [self doActualReload];
        });
    });
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[self.myCachedHeightArray objectAtIndex:indexPath.row] floatValue];
}
于 2013-01-08T08:17:38.093 に答える
1

I put this into my custom cell subclass usually... so the code doesn't clutter my controller and is correct for the cell I use. (+ it is better suited to MVC that way... the height of a cell is a view property IMO)

I DONT measure with each cell, but with a static method - see https://github.com/Daij-Djan/TwitterSearchExampleApp (the ViewController and the DDTweetTableViewCell class for an example)

于 2013-01-08T08:26:36.763 に答える
0

self.tmpCell をどのように設定/取得しますか? 再利用可能なセルをプロパティに保存しますか?

セルからテキストを取得してサイズを計算する代わりに、セルのデータ ソースからテキストのサイズを計算できます。つまり、テキストをcellForRowAtIndexPath:何らかの形で(たとえば、配列から)設定し、そのテキストを使用して計算するだけです。

フレームの場合: セルは tableview の同じ幅を持っています フォントの場合: というメソッドを書くだけです

- (UIFont *)fontForCellAtIndexPath:(NSIndexPath *)indexpath

からも使用しcellForRowAtIndexPathます。後でテキストのフォントを変更すると、作業が簡単になります。

于 2013-01-08T07:57:31.033 に答える
0

フロントエンドでこれを行う必要がありますが、一度だけ行い、結果を配列にキャッシュして、それを将来の呼び出しに使用します。

つまり、配列値が初めて空なので、それを計算して配列に格納します。

次に配列に値があるときは、計算せずにそれを使用します。

于 2013-01-08T07:49:56.697 に答える