1

テーブルビュー内にカスタムセルを作成し、画像があり、背が高く、テキストだけのセルを作成します。セルの高さは heightForRowAtIndexPath で計算されます。これは cellForRowAtIndexPath が呼び出される前に行われると思います。高さに関係なくセルの下部にイメージビューを配置したいのですが、cellForRowAtIndexPath 内から計算された高さを取得する方法がわかりません。

4

3 に答える 3

15

返事が遅くなりました..

しかし、@ user216661 が指摘したように、Cell または ContentView の高さを取得する際の問題は、セルの元の高さを返すことです。高さが可変の行の場合、これは問題です。

より良い解決策は、セル ( ) の Rect を取得しrectForRowAtIndexPathてから、そこから高さを取得することです。

- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
    UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
    if (aCell == nil) {
        CGFloat aHeight = [iTableView rectForRowAtIndexPath:iIndexPath].size.height;
        // Use Height as per your requirement.
    }

    return aCell;
}
于 2013-07-28T10:38:38.237 に答える
3

デリゲートに尋ねることはできますが、tableView が既に要求し、それに応じてセルのサイズを設定しているため、2 回要求することになります。細胞自体から調べた方がいいです...

// in cellForRowAtIndexPath:, deque or create UITableViewCell *cell
// this makes the call to heightForRow... and sizes the cell
CGFloat cellHeight = cell.contentView.bounds.size.height;

// alter the imageView y position (assuming the rest of the frame is correct)
CGRect imageFrame = myImageView.frame;
imageFrame.y = cellHeight - imageFrame.size.height;   // place the bottom edge against the cell bottom
myImageView.frame = imageFrame;
于 2012-08-21T22:10:02.707 に答える
-1

自分で heightForRowAtIndexPath を呼び出すことができます! cellForRowAtIndexPath から indexPath を引数として渡すだけで、設定しているセルの高さを知ることができます。

UITableViewController を使用していると仮定すると、これを cellForRowAtIndexPath 内で使用するだけです...

float height = [self heightForRowAtIndexPath:indexPath]
于 2012-08-21T21:23:47.627 に答える