Getting the Row for Section indexPath in a tableView

23,290

Solution 1

Hi this is enough try and let me know if you face any issues...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

[self.tableView deselectRowAtIndexPath:indexPath animated:NO];

NSArray * nameArray = [self.sectionsArray objectAtIndex:indexPath.section];

PFUser *user = (PFUser*)[nameArray objectAtIndex:indexPath.row];

// PFUser *user = (PFUser*)[self.friends objectAtIndex:indexPath.section];

if (cell.accessoryType == UITableViewCellAccessoryNone) 
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.recipients addObject:user.objectId];
NSLog(@"added:%@",user);
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
    [self.recipients removeObject:user.objectId];
    NSLog(@"removed:%@",user);
}
// this is enough
}

Solution 2

Just like the code Spynet suggested only a little bit different:

NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
    PFUser *user = [array objectAtIndex:indexPath.row];

Using that code I was able to get the proper indexPath for row in each section. Also, in order to get the cells accessory checkmarks to work correctly I had to create a newIndexPath using the section and row values for each index. My final code is here:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    int row = indexPath.row;
    int section = indexPath.section;

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
    NSLog(@"newIndexPath: %@", newIndexPath);

    [self.tableView deselectRowAtIndexPath:newIndexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];

    NSLog(@"sectionsArray:%@",self.sectionsArray);

    NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
    PFUser *user = [array objectAtIndex:indexPath.row];
    NSLog(@"array:%@",array);
    NSLog(@"user:%@",user);

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user];
        NSLog(@"recipients:%@",self.recipients);
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user];
        NSLog(@"recipients:%@",self.recipients);
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];
}
Share:
23,290
Jared Gross
Author by

Jared Gross

Updated on May 07, 2020

Comments

  • Jared Gross
    Jared Gross about 4 years

    Recently I put implemented an indexing feature for the user names in my app. I have been trying to access the rows in each section and add them to my recipients array when the user taps the index path. I have tried passing many different values in my objectAtIndex: method but none seem to be working. I am able to log the correct index row and section in indexPathForRow:inSection: method but the return value is an indexPath and not an integer. So something with my objectForIndex: method is obviously not right. Any help or references would be super. cheers!

    This line:

    PFUser *user = [self.friends objectAtIndex:indexPath.section];

    Returns only the first username in each section, no matter what name I tap in the section. So I need to access the Row in addition, not just the section.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    int section = indexPath.section;
    int row = indexPath.row;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
    NSLog(@"newIndexPath: %@", newIndexPath);
    
    PFUser *user = [self.friends objectAtIndex:indexPath.section];
    
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user.objectId];
    NSLog(@"added:%@",user);
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user.objectId];
        NSLog(@"removed:%@",user);
    }
    
    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];
    }
    
    • iPatel
      iPatel over 10 years
      if you want to get selected row's number and section then you just need to write indexPath.row and indexPath.section ... no need to do any things else :) and also only indexPath dose not return integer value.. :)
    • Arun
      Arun over 10 years
      what is your data source.....
    • Jared Gross
      Jared Gross over 10 years
      @iPatel but how would I pass both of those values in for PFUser *user = [self.friends objectAtIndex:indexPath.section];?
    • Arun
      Arun over 10 years
      PFUser *user = [self.friends objectAtIndex:indexPath.section];// What is your model of data source.... is it array of object or array of array of objects
    • iPatel
      iPatel over 10 years
      What is PFUser *user and also your question bit more create confusion please can you elaborate it ??
    • Jared Gross
      Jared Gross over 10 years
      @Spynet I have the names stored in section arrays in a collation.
    • Jared Gross
      Jared Gross over 10 years
      PFUser is an object being stored in the backend on Parse. It has keys for username, date, relation, etc.
    • Arun
      Arun over 10 years
      u have only one section ?.... can u add code for number of section method???
    • Arun
      Arun over 10 years
      please post the section code ......
    • Jared Gross
      Jared Gross over 10 years
      quite new to this and 100% self taught. so I'm not entirely sure I'm doing everything correctly. But since I am able to log the correct section and row.. I feel like I can't be too far off. I hope.
    • Arun
      Arun over 10 years
      NSIndexpath is the class please read the documentaion developer.apple.com/library/mac/documentation/Cocoa/Referenc‌​e/…
    • Arun
      Arun over 10 years