简体   繁体   中英

Getting the Row for Section indexPath in a tableView

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]);
    }
}];
}

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
}

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]);
        }
    }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM