4

各 UITableViewCell の高さを設定します

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

各 UITableViewCell の高さを取得する必要があります

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

別のセルにSubViewを追加する必要があるためです。

cell.frame& cell.bounds&を使用しようとしましcell.contentView.frameたが、高さが変わりません。

4

5 に答える 5

8

これでは遅すぎるかもしれませんが、デリゲート メソッドを使用できます。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect cellSize = cell.frame;
}

それが誰かに役立つことを願っています。:)

于 2013-09-01T09:23:27.207 に答える
2

私はこのように解決しました:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // create object
    id myObj = [_arrayEvents objectAtIndex:indexPath.row];

    // create static identifier
    static NSString * myIdentifier = @"Cell";

    // create static UITableViewCell
    static UITableViewCell * cell;

    // create cell with object
    cell = [self buildCell:myIdentifier tableView:tableView obj: myObj];

    return cell.contentView.frame.size.height;

}
于 2013-11-29T18:23:28.050 に答える
1

それらは異なるものです。heightForRowAtIndexPath: では、対応するセルを表示するために使用する垂直方向のスペースを UITableView に伝えますが、これはセルの実際のサイズには影響しません! したがって、それらを一致させたい場合は、寸法を手動で設定する必要があります。

セルフレームのサイズを変更したくない/必要がない場合は、カスタムセルクラスのプロパティに高さを保存するだけです。

于 2012-12-20T08:59:51.297 に答える
-1

@ilMalvagioDottorProsci が言うように、heightForRowAtIndexPath 関数によって返される値は、セルの実際のサイズには影響しません。

各行の高さがセルの高さによって決定されるようにしたい場合は、トリックがあります。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id aKey = [self buildCellCacheKey:indexPath];
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

    if ([_cellCache objectForKey:aKey]==nil) {
        [_cellCache setObject:cell forKey:aKey];
    }

    return cell.bounds.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // for flexible height of table view cell.in this way,the cell height won't be caculate twice.
    id aKey = [self buildCellCacheKey:indexPath];
    UITableViewCell *cacheCell = [_cellCache objectForKey:aKey];
    if (cacheCell) {
        [cacheCell retain];
        [_cellCache removeObjectForKey:aKey];
        LOGDebug(@"return cache cell [%d]",indexPath.row);
        return [cacheCell autorelease];
    }

// here is the code you can config your cell..  
}
于 2012-12-20T09:12:11.273 に答える