correct indexPath.row selection in TableView which has multiple sections?

前端 未结 2 1894
忘了有多久
忘了有多久 2021-01-15 12:19

i am having tableview with multiple sections .each section is having different no of rows. when i select in particular row in particular section, how can i find correct inde

2条回答
  •  别那么骄傲
    2021-01-15 12:53

    I don't know what you mean by "correct". The indexPath.row is always local to the section, which means

    section 0
    ---------
    [ row 0 ]
    [ row 1 ]
    [ row 2 ]
    ---------    
    section 1
    ---------
    [ row 0 ]
    [ row 1 ]
    ---------
    section 2
    ---------
    [ row 0 ]
    [ row 1 ]
    [ row 2 ]
    [ row 3 ]
    ---------    
    

    To get the global row, you can compute a partial sum.

    NSUInteger row = 0;
    NSUInteger sect = indexPath.section;
    for (NSUInteger i = 0; i < sect; ++ i)
       row += [dataSource tableView:tableView numberOfRowsInSection:i];
    row += indexPath.row;
    return row;
    

提交回复
热议问题