3

我在 Interface Builder 的 UITableView 中组成了几个不同的自定义单元格,包括每个单元格都有一个自定义高度,大于默认的 44 像素高。

然后我像这样加载它们:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier;

    for (int cell = 0; cell <= [tableCellsArray count]; cell++) 
    {
        if ([indexPath row] == cell) 
        {
            CellIdentifier = [tableCellsArray objectAtIndex:cell];
        }
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    return cell;
}

它们每个都有一个自定义类,在上面的代码中,我遍历一个数组,该数组基本上保存了相关的单元格标识符。然而,在运行应用程序时,所有单元格都返回默认的 44 像素高度。

如果不使用委托方法返回我自己的单元格高度,我是否没有遗漏可能导致这种情况的东西?

谢谢。

4

3 回答 3

6

您可以在 tableView 的定义中更改所有单元格的高度,但要单独设置它们,您必须使用委托方法。您可以在 IB 中设置它令人困惑,但它仅用于显示目的,而不是在运行时使用。

于 2012-04-06T15:02:32.160 回答
5

您将必须实现以下选择器并应用正确的逻辑,以便根据您的自定义单元格类型设置正确的高度。没有它,将使用默认高度 44

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

    return YOUR_WANTED_CELL_HEIGHT;
}

不要以为你错过了什么。

于 2012-04-06T14:56:03.470 回答
0

iOS 8及更高版本中,我们可以使用Dynamic Table View Cell Height

使用这个特性UITableviewCell从它的内容中获取它的高度,我们不需要写heightForRowAtIndexPath

我在viewDidLoad()中要做的所有事情

tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;

单元格中的设置约束:

在此处输入图像描述

结果:

在此处输入图像描述

在这里我们可以看到:当文本增加时,单元格大小也会自动增加,而无需编写heightForRowAtIndexPath

于 2016-02-02T07:03:32.627 回答