16

I have a UITableView, with multiple sections in it, and each section has multiple rows. I want to get the row number of selected cell with respect to the entire table and not the section only.

example:

  1. I have two sections in the UITableView, section 1 has 3 rows and section 2 has 5 rows.
  2. When I select sections 2's 2nd row, I should get the 5 as the row number in didSelectRowAtIndexPath method, rather than getting 2 as the row number (which is with respect to the section).

I tried to get the row number myself by doing the following, but it does not seem to work:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

NSLog(@"%d",indexPath.row);

int theRow =  indexPath.row;

NSLog(@"%d",theRow);
}

I was thinking of storing the row number in an intvariable, and then add row numbers to it myself, but the code crashes when trying to store indexpath.row in theRow .

Please help. Thank you

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hyder
  • 1,163
  • 2
  • 13
  • 37
  • 3
    You own the model, so you tell the table view how many rows it has. Just add everything up. – Wain Jun 06 '13 at 17:38
  • @Wain Can you explain a little more? I am sorry, but I didn't get what you said. – Hyder Jun 06 '13 at 17:40
  • 2
    @SHA: Can you explain why you want to work with an "absolute" row number instead of a "section relative" row number? – Martin R Jun 06 '13 at 17:42
  • @MartinR: To get a value from that (abosulte row number) index of an Array. – Hyder Jun 06 '13 at 17:47
  • 2
    @SHA: OK, but you might also consider to use a *nested array* (with the section number as first index and the row number as second index) as data source. – Martin R Jun 06 '13 at 17:51

7 Answers7

44
NSInteger rowNumber = 0;

for (NSInteger i = 0; i < indexPath.section; i++) {
    rowNumber += [self tableView:tableView numberOfRowsInSection:i];
}

rowNumber += indexPath.row;
Wain
  • 118,658
  • 15
  • 128
  • 151
10

Here's a cleaner implementation of this concept in Swift:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    var rowNumber = indexPath.row
    for i in 0..<indexPath.section {
        rowNumber += self.tableView.numberOfRowsInSection(i)
    }

    // Do whatever here...
}
Max Masnick
  • 3,277
  • 2
  • 27
  • 28
  • Thanks for the answer. just an issue as a renamed edit: numberOfRowsInSection(i) renamed as numberOfRows(inSection: i). – Burcu Kutluay Mar 02 '21 at 09:56
9

Here is a Swift adaption of the above answer by Wain

class func returnPositionForThisIndexPath(indexPath:NSIndexPath, insideThisTable theTable:UITableView)->Int{

    var i = 0
    var rowCount = 0

    while i < indexPath.section {

        rowCount += theTable.numberOfRowsInSection(i)

        i++
    }

    rowCount += indexPath.row

    return rowCount
}
PJeremyMalouf
  • 613
  • 6
  • 15
2

I had a big data set and the previous answers using for loops was causing performance issues for me in the lower sections. I ended up doing the calculating beforehand and sped things up a bit.

private var sectionCounts = [Int:Int]()

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let number = fetchedResultsController.sections?[section].numberOfObjects ?? 0
    if section == 0 {
        sectionCounts[section] = number
    } else {
        sectionCounts[section] = number + (sectionCounts[section-1] ?? 0)
    }
    return number
}

func totalRowIndex(forIndexPath indexPath: NSIndexPath) -> Int {
    if indexPath.section == 0 {
        return indexPath.row
    } else {
        return (sectionCounts[indexPath.section-1] ?? 0) + indexPath.row
    }
}
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
1

The following is a Swiftier version of @Wain solution:

let rowNumber = Array(0..<indexPath.section).reduce(0) { $0 + tableView.numberOfRows(inSection: $1) } + indexPath.row
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
0

Let say I implement the code below in didSelectRowAt / didSelectItemAt callbacks ...

UITableView

var index: Int = indexPath.row
for i in 0..<indexPath.section {
    index += tableView.numberOfRows(inSection: i)
}

UICollectionView

var index: Int = indexPath.item
for i in 0..<indexPath.section {
    index += collectionView.numberOfItems(inSection: i)
}

Example: 2 sections, 3 rows(items) each. Selected row(item) 1 in the section 1, index = 4

rusito23
  • 995
  • 8
  • 20
Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
-4

i dont know about the multiple sections but i can give you for the one section...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger index=indexPath.row;
NSString *string=[[NSString alloc]initWithFormat:@"%ld",(long)index];
}

from this you can get the row number and you can save it to the string....

Smit
  • 1