0

我有一个 UITableViewController 具有不同的部分和行,我以编程方式创建它们,我希望每个部分都有特定的高度

请让我知道如何为每行部分设置特定的高度?

提前致谢!

正如你所看到的,因为我使用了 heightForRowAtIndexPath:我的部分中的每一行都有相同的高度,但我希望每个部分都有特定的高度

![在此处输入图像描述][2]

我知道我们有heightForHeaderInSectionheightForFooterInSection但我如何将几个部分的特定高度放在一张表中

这是代码:

编辑:

如果我使用此代码,那么我的部分之间有很大的空间,但我希望在不同部分的每一行都有特定的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
switch (section) {
    case 0:
        return 40;
        break;
    case 1:
        return 40;
        break;
    case 2:
        return 80;
        break;
    default:
        return 40;
        break;
}
}
4

2 回答 2

2

根据截面为不同的情况返回不同的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return your_req_height;
            break;            
        default:
            break;
    }
}

NSIndexPath提供行和部分,因此您可以为部分中的每一行返回不同的高度。

应该补充的是,这种方法看起来很脆弱,因为您要将特定的部分/行对绑定到固定高度。您应该使用标记来设置表格项,该标记指示每个部分/行对中的内容类型。

于 2012-08-16T09:45:18.197 回答
1

您需要添加此代码。这将为您的 CheckBoxCompensationViewCell 类型单元格返回自定义高度。只需将 your_required_height 替换为 CheckBoxCompensationViewCell 的高度。我试过60。

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

switch (indexPath.section) {

//Since you have custom cells i-e CheckBoxCompensationViewCell type cells in this section, you need to return custom cell height in tableview
case 2:
    return 60; //i am supposing your_required_height is 60
    //return your_required_height; //height of CheckBoxCompensationViewCell
    break;

default:
    return 44; //default_height of tableview cell
    break;
    }
}
于 2012-08-16T12:33:38.727 回答