分组UITableview中的单元格减少

时间:2013-08-18 15:44:23

标签: ios objective-c uitableview

我正在向现有tableView添加一个部分并获取此内容:

enter image description here

我的新细胞减少了高度。适当的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cells[indexPath.section][indexPath.row];
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ([headers[section] isKindOfClass:[UIView class]])
        return [headers[section] frame].size.height;

    return 10.0f;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if ([headers[section] isKindOfClass:[UIView class]])
        return headers[section];

    return nil;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = cells[indexPath.section][indexPath.row];

    if (cell == clientXibCell) return 100.0f;
    if (cell == agencyXibCell) return 145.0f;
    return 46.0f;
}

我无法理解我需要做些什么来解决这个问题。问题来源可以是什么想法?

更新 我现在确定预定义的自定义单元格可视界面会给这个问题带来麻烦。

supervisorCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    bgView = [[UIImageView alloc] initWithFrame:supervisorCell.backgroundView.frame];
    [bgView setImage:stretchableImageByHorizontal([UIImage imageNamed:@"cell_bgd_bottom"])];
    [supervisorCell setBackgroundView:bgView]; 
    bgView = [[UIImageView alloc] initWithFrame:supervisorCell.backgroundView.frame];
    [bgView setImage:stretchableImageByHorizontal([UIImage imageNamed:@"cell_bgd_bottom_active"])];
    [supervisorCell setSelectedBackgroundView:bgView];

当我取消除了创建单元格的第一个语句之外的所有内容时,除了单元格的自定义外观之外,一切正常。在这个简单的代码中我需要更改什么才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

细胞的高度由heightForRowAtIndexPath:控制。看一下你的代码,似乎这个方法总是返回46

你的两个ifs正在比较指针,i。例如,细胞的瞬间。这意味着您的所有单元格中,其中一个将具有高度100,一个145以及所有其他46.f

我认为您要完成的是为同类型的所有单元格设置此高度,因此您应该更改heightForRowAtIndexPath:方法,如下所示:

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

    if ( [cell isKindOfClass:[YourCustomCell1 class]] ) return 100.0f;
    if ( [cell isKindOfClass:[YourCustomCell2 class]] ) return 145.0f;
    return 46.0f;
}

Ps1:为您自己的班级更改YourCustomCell班级。如果您没有子类,请尝试设置标签或类似的东西以区分它们。

Ps2:始终使用tableview的方法cellForRowAtIndexPath来获取indexPath对单元格的引用。