自定义UITableViewCell高度

Jos*_*ane 3 iphone objective-c uitableview

我在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;
}
Run Code Online (Sandbox Code Playgroud)

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

不使用委托方法返回我自己的单元格高度,是不是我错过了可能导致这种情况的东西?

谢谢.

lna*_*ger 6

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


Fra*_*ank 5

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

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

    return YOUR_WANTED_CELL_HEIGHT;
}
Run Code Online (Sandbox Code Playgroud)

不要以为你错过了其他任何东西.