5

100 を返す heightForRowAtIndexPath: メソッドを使用してセルの高さを設定しています。しかし、cellForRowAtIndexPath メソッドでは、セルのフレームの高さは常に 44px です。

以下は私のコードです。

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    [cell setBackgroundColor:[UIColor redColor]];
    [cell.contentView setBackgroundColor:[UIColor redColor]];
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    NSLog(@"%f %f", cell.frame.size.height, cell.contentView.frame.size.height);
    return  cell;
}

NSLog ステートメントは「44px 44px」を出力します。

このステートメントを使用して正しい高さを取得できます

CGFloat height = [tableView heightForRowAtIndexPath:indexPath];
4

1 に答える 1

5

セルの割り当て中にデリゲートメソッドは呼び出されません。initWithStyleサブクラスで上書きしない限り、常に高さ44のセルを返します。

于 2013-01-24T15:08:59.877 に答える