【问题标题】:Setting each cell in its own group将每个单元格设置在自己的组中
【发布时间】:2010-11-01 00:35:15
【问题描述】:

我的 UITableViewCells 占据了整个查看区域。我想使用一个分组表,以便单元格看起来有圆角。现在,如果我将部分的数量设置为大于 1,我将获得分组样式的部分数量,但所有单元格在每个部分中重复。如何设置表格,使每个单元格都在一个部分中?

另外,是否可以通过编程方式将表格样式设置为分组?

【问题讨论】:

    标签: iphone cocoa-touch uitableview


    【解决方案1】:

    第二个问题:

    tableView.style = UITableViewStyleGrouped;
    

    或者,如果您以编程方式创建它:

    tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    

    对于您的第一个问题,我假设您正在设置您的单元格,如下所示:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        ...
        setupCell(cell,indexPath.row);
        return cell;
    }
    

    其中 setupCell 是检查索引并相应地设置单元格的东西。然而,IndexPath 会告诉您要设置哪一行在哪个部分

    我还假设您要返回函数中的全部行数

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    

    您应该只返回 1,因为您希望每个部分恰好有 1 行。

    然后在设置您的单元格时,您需要检查indexPath.section 而不是indexPath.row。行按部分从零开始,因此对 cellForRowAtIndexPath 的调用将具有如下所示的索引路径:

    部分行
    0, 0
    1, 0
    2, 0

    而最初,当您只有一个部分时,它会是:
    0, 0
    0, 1
    0, 2

    您现在看到的是,因为您返回的行数与部分相同:
    0, 0
    0, 1
    0, 2

    1, 0
    1, 1
    1、2

    2, 0
    2、1
    2, 2

    【讨论】:

      【解决方案2】:

      实际上,你不能这样做:

      tableView.style = UITableViewStyleGrouped;

      因为 style 是只读属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        • 2017-12-07
        • 1970-01-01
        • 2019-05-04
        • 1970-01-01
        • 2020-09-17
        • 1970-01-01
        相关资源
        最近更新 更多