[英]selectedIndexPath in sectioned UITableView
As you can see in the picture linked below, when I select the first row, the selected row index returns 0. When I select the second row it also returns 0 and when I select the third row it returns 1. 如您在下面的链接图中看到的那样,当我选择第一行时,所选行的索引将返回0。当我选择第二行时,它也会返回0,而当我选择第三行时,它将返回1。
Why is this and how can I determine what is actually selected? 为什么会这样,如何确定实际选择的内容?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *selectedRowIndexPath = [tableView indexPathForSelectedRow];
int selectedSection = selectedRowIndexPath.section;
NSInteger i =0;
for (NSInteger j = 0; j <= selectedSection; ++j)
{
for (i=0; i < [tableView numberOfRowsInSection:j]; ++i)
{
}
}
}
It is because you have to look at the sections. 这是因为您必须查看这些部分。
It is indeed first row but of the section 0, section 1 etc. 它确实是第一行,但在第0节,第1节等中。
To generalize, you have rows 0 as much as you have sections(assuming you have rows in each section). 概括地说,行0和段数一样多(假设每个段中都有行)。
Section 0(Let say A since you have something like phone book): 第0节(因为您有电话簿之类,所以请说A):
Section 1(Letter b) 第1节(字母b)
Section 2(letter c): 第2节(字母c):
Code: 码:
NSInteger rowNumber = 0;
for (NSInteger i = 0; i < indexPath.section; i++) {
rowNumber += [self tableView:tableView numberOfRowsInSection:i];
}
rowNumber += indexPath.row;
They are different section. 他们是不同的部分。
Row begin with index 0 in every section. 行在每个节中以索引0开头。
IndexPath has two component:section and row. IndexPath具有两个部分:节和行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.