1

heightForRowAtIndexPath以前に呼び出されたのでcellForRowAtIndexPath、内部から高さを変更したい場合は、変更cellForRowAtIndexPathできると思いました。できないようです。を介してテストしましたが、NSLog変更するcell.frame.size.heightと、変更はそこに適切に保存されますが、セル自体は新しいサイズになりません(設定したサイズを使用していheightForRowAtIndexPathます)。

後のある時点で呼び出されるセルの高さを調整する別の方法はありcellForRowAtIndexPathますか?そうでない場合、これを回避する別の方法はありますか?cellForRowAtIndexPath各セルに順番に画像をランダムに追加するかどうかをその場で決定しているので、使用する必要があります。

4

4 に答える 4

6

UITableViewDelegate heightForRowAtIndexPathUITableView rowHeightセルの高さを指定するための唯一のメカニズムです。テーブルビュー自体は、これらに基づいてセルのサイズを変更しています。自分でセルフレームを設定して、それが機能することを期待することはできません。

セルを作成する前に、セルの高さを事前に計算できるようにするのが最善の方法です。+ (CGFloat) cellHeightWithDatum: (id) datum forWidth: (CGFloat) tableWidthからセルクラスを呼び出すメソッドを定義することがよくありますheightForRowAtIndexPath。ここでのデータムは、セルのコンテンツを駆動するモデルです。次に、このメソッドはモデルを調べて、セルの高さが必要かどうかを判断します。

セルの作成後にセルの高さをどうしても変更する必要がある場合は、テーブルビューにセルの再読み込みを要求するか、reloadDataを呼び出さずにテーブル全体を更新することで変更できます。このトリックは次のように行われます。

[tableView beginUpdates];
[tableView endUpdates];

単一のセルをリロードするには: UITableView reloadRowsAtIndexPaths:withRowAnimation:

于 2013-02-28T16:32:36.007 に答える
2

heightForRowAtIndexPath代わりに、メソッドに画像を追加することをランダムに決定するコードを配置する必要があります。

を使用して、画像を使用するNSMutableDictionaryを追跡しNSIndexPathます。

于 2013-02-28T16:24:19.433 に答える
0

拡張する必要のあるインデックスを追跡する必要があります。

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

    if([self shouldBeExpanded:indexPath.row]) { //method that checks if this cell should be expanded. You'll need to check the array if it contains the row.
        return kCellHeightExpanded;
    }

    return kCellNormalHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     //set cell to be expanded
     [self expandCellAt:indexPath.row]; //method that sets which cell is expanded. You'll need to implement this yourself. Use an array to track which row should be expanded

     //this causes heightForRowAtIndexPath: to be called again
     [tableView beginUpdates];
     [tableView endUpdates];
}
于 2013-02-28T16:36:59.727 に答える
0

cellForRowAtIndexPathの前にランダムに決定し、各indexPathのdataSourceのデータ構造に画像があるかどうかにフラグを格納する必要があります。画像があるかどうかを判断する作業が安価な場合は、heightForRowAtIndexPathでそれを行うことができます。このようなもの:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL showingAnImage = arc4random() % 2;
    // self.imageIndexPaths is some data structure that lets you associate a value to an index path
    self.imageIndexPaths[indexPath] = @(showingAnImage);
    if (showingAnImage){
        // return height of a cell with an image
    } else {
        // return height of a cell without an image
    }
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if ([self.imageIndexPaths[indexPath] boolValue]){
        // Configure for image
    } else {
        // Configre without image
    }
    return cell;
}

cellForRowAtIndexPath:でセルのサイズを変更しても機能しません。

于 2013-02-28T16:26:49.570 に答える