4

最近我在我的应用程序中为用户名实现了索引功能。当用户点击索引路径时,我一直在尝试访问每个部分中的行并将它们添加到我的收件人数组中。我尝试在我的 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]);
    }
}];
}
4

2 回答 2

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
}
于 2013-10-29T08:02:50.840 回答
2

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

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

使用该代码,我能够为每个部分中的行获得正确的 indexPath。此外,为了使单元格附件复选标记正常工作,我必须使用每个索引的部分和行值创建一个 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]);
        }
    }];
}
于 2013-10-30T23:01:29.893 回答