-1

Is it possible to get the section count for the respective selected row in a UITableView

Abhishek
  • 184
  • 12

5 Answers5

0

yes you can check for section with

indexPath.section
Devang Tandel
  • 2,988
  • 1
  • 21
  • 43
0

If you have NSIndexPath you can get row by

indexPath.row 

or section by

indexPath.section
Lifeplus
  • 531
  • 2
  • 9
  • 22
0

Try the following sample code:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rows = 0;

    switch (section) {
        case 0:
            rows = 7;
            break;
        case 1:
            rows = 21;
            break;

        case 2:
            rows = 3;
            break;

        case 3:
            rows = 1;
            break;

        case 4:
            rows = 5;
            break;

        default:
            break;
    }
    return rows;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];

    // Configure the cell...

    cell.textLabel.text = [NSString stringWithFormat:@"section : %ld & rows : %ld",(long)indexPath.section,(long)indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //This will print which row of which section is clicked
    NSLog(@"section : %ld & rows : %ld",(long)indexPath.section,(long)indexPath.row);

}
Dinesh
  • 1,137
  • 9
  • 14
0

I solved what i was looking for. I wanted to get all the section number for the respective selected rows in it, on the button click.

I used NSArray *paths = [self.tableView indexPathsForSelectedRows];

Abhishek
  • 184
  • 12
0

which is right !

   NSArray <NSIndexPath *>*rows = [tableView indexPathsForSelectedRows];