在tableView中获取section index的行

时间:2013-10-29 05:41:39

标签: ios indexing row tableview

最近我在我的应用程序中为用户名实现了索引功能。我一直在尝试访问每个部分中的行,并在用户点击索引路径时将它们添加到我的收件人数组中。我尝试在objectAtIndex:方法中传递许多不同的值,但似乎都没有工作。我能够在indexPathForRow:inSection:方法中记录正确的索引行和节,但返回值是indexPath而不是整数。所以使用我的objectForIndex:方法显然不对。任何帮助或参考都是超级的。干杯!

这一行:

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

只返回每个部分中的第一个用户名,无论我在该部分中点击什么名称。所以我需要另外访问Row,而不仅仅是部分。

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

2 个答案:

答案 0 :(得分:4)

嗨,这是足够的尝试,如果您遇到任何问题,请告诉我......

- (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
}

答案 1 :(得分:3)

就像代码Spynet建议的只是有点不同:

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

使用该代码,我能够为每个部分中的行获取正确的indexPath。此外,为了使单元格附件检查标记正常工作,我必须使用每个索引的section和row值创建newIndexPath。我的最终代码在这里:

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