简体   繁体   English

UITableView中的selectedIndexPath

[英]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? 为什么会这样,如何确定实际选择的内容?

picture 图片

-(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):

  • Abraham Lincoln(row 0) 亚伯拉罕·林肯(第0行)
  • Aladin with the lattern(row 1) 阿拉丁与拉特(第1行)
  • Abrakadabra(Row 2) 亚喀巴巴拉(行2)

Section 1(Letter b) 第1节(字母b)

  • Bob(row 0) 鲍勃(第0行)
  • Brandley(row 1) 布兰德利(第1行)

Section 2(letter c): 第2节(字母c):

  • Cindy(row 0) 辛迪(第0行)
  • Cinemon(row 1) 电影院(第1行)
  • Cayonara(row 2) Cayonara(第2行)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM