Is it possible to get the section count for the respective selected row in a UITableView
Asked
Active
Viewed 375 times
-1
-
`NSIndexPath` contains both `section` and `row` information. – vadian Apr 14 '16 at 10:58
-
Possible duplicate of [Get Selected index of UITableView](http://stackoverflow.com/questions/4030811/get-selected-index-of-uitableview) – Guillaume Algis Apr 14 '16 at 10:58
5 Answers
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];

Otherplayer
- 1
- 1