我有一个tableview,其中包含一个用户名的列表,这些列表以字母顺序为单位索引到各节和行。当我点击一节中的一行时,将正确的用户添加到我的收件人数组中,并且检查标记是单元格中的位置。但是,在其他未选择的用户名旁边也显示了一个检查标记,不在收件人数组中。我试图使用新的Indexpath重新分配选定的单元格(请参见下面的代码),但无法使其正常工作。它注册正确的路径,但不会分配。我正在使用类似的方法来分配用户每个部分中的行,但由于某种原因,附件标记给我带来了问题。我已经看到了有关同一主题的其他一些线程,但可以清洗;可以解决我的情况。有线索吗?干杯!

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

    int row = indexPath.row;
    int section = indexPath.section;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];

    [tableView deselectRowAtIndexPath:newIndexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];

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

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

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];

这是CellForrowatIndExpath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Get the user names from the array associated with the section index in the sections array.
    NSArray *userNamesInSection = (self.sectionsArray)[indexPath.section];

    // Configure the cell with user name.
    UserNameWrapper *userName = userNamesInSection[indexPath.row];
    cell.textLabel.text = userName.user;

    return cell;
}
有帮助吗?

解决方案

如我所见,您在CellForrowatIndExpath中犯了2个错误,该错误未检查单元格是否为null创建一个错误并根据收件人列表设置单元格。

您应该喜欢以下:

NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

PFUser *user = [self getUserAtIndexPath:indexPath];
cell.textLabel.text = user.name;

if ([self.recipients containObject:user]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top