将每个单元格设置在其自己的组中 [英] Setting each cell in its own group

查看:89
本文介绍了将每个单元格设置在其自己的组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UITableViewCells占用了完整的查看区域。我想使用一个分组表,使单元格显示为圆角。现在,如果我将部分的数量设置为大于1的部分,我得到分组样式的部分数量,但所有单元格在每个部分重复。

My UITableViewCells take up the full viewing area. I'd like to use a grouped table so the cells appear to have rounded corners. Right now, if I set the number of sections to something greater than one, I get the number of sections in grouped style but all the cells repeat in each section. How do I setup the table so each cell is in a section?

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

Also, is it possible to programmatically set the table style to grouped?

推荐答案

您的第二个问题:

tableView.style = UITableViewStyleGrouped;

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

Or, if you're creating it programmatically:

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

对于你的第一个问题,我假设你正在设置你的单元格这样:

For your first question, I assume you are setting up your cells something like this:

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

其中setupCell是检查索引并相应设置单元格的内容。但是,IndexPath会告诉您要设置哪一行

Where setupCell is something that checks the index and sets the cell accordingly. The IndexPath, however, tells you which row in which section to set up.

我还假设您返回的是函数中的行

I also assume that you're returning the full number of rows in the function

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

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

Where you should only be returning 1, since you want exactly 1 row in each section.

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

Then when setting up your cell, you'd want to check the indexPath.section instead of the indexPath.row. Rows are zero-based by section, so the calls to cellForRowAtIndexPath will have index paths that look like this:

栏目行

0,0

1,0

2,0

Section Row
0, 0
1, 0
2, 0

当您只有一个部分时,它将是:

0,0

0,1

0,2

Whereas originally, when you had only one section, it would have been:
0, 0
0, 1
0, 2

和你现在看到的,因为你返回相同的行数作为部分:

0,0

0,1

0,2

And what you're seeing right now, since you return the same number of rows as sections is:
0, 0
0, 1
0, 2

1,0

1,1

1,2

1, 0
1, 1
1, 2

2,0

2,1

2,2

2, 0
2, 1
2, 2

这篇关于将每个单元格设置在其自己的组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆