2

I'm using a tableview with sections and multiple selection, but I have an issue with multiple rows being checked when one row is chosen... I've seen a few other threads about this, but didn't really get a solution. ..

这是我的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{    
    [employeeTable deselectRowAtIndexPath:[employeeTable indexPathForSelectedRow] animated:NO];

    UITableViewCell *cell = [employeeTable cellForRowAtIndexPath:indexPath];    

    // get the letter in each section
    NSString *alphabet = [charIndex objectAtIndex:indexPath.section];

    // get the names beginning with the letter
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];    

    NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];    

    NSString *name = [names objectAtIndex:indexPath.row];

    for(int i = 0; i < [employeeConnection.employees count]; i++)
    {
        Employee *aEmployee = [employeeConnection.employees objectAtIndex:i];

        NSString *firstName = aEmployee.firstName;
        NSString *lastName = aEmployee.lastName;
        NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

        if([fullName isEqualToString:name])
        { 
            NSLog(@"Name: %@", name);

            if (cell.accessoryType == UITableViewCellAccessoryNone) {

                cell.accessoryType = UITableViewCellAccessoryCheckmark;

                // Reflect selection in data model
                [chosenEmployees addObject:aEmployee.employeeID];
                [chosenEmployeesNames addObject:name];

            } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {

                cell.accessoryType = UITableViewCellAccessoryNone;

                // Reflect deselection in data model
                [chosenEmployees removeObject:aEmployee.employeeID];
                [chosenEmployeesNames removeObject:name];
            }
        }
    }
}

更新:添加了 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        cell.textLabel.textColor = [UIColor whiteColor];
    }

    // Get the letter in the current section
    NSString *alphabet = [charIndex objectAtIndex:[indexPath section]];

    // Get the names beginning with the letter
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];

    if([names count] > 0)
    {
        // Extract the name
        cell.textLabel.text = [names objectAtIndex:indexPath.row];
    }

    return cell;
}
4

2 回答 2

10

我建议存储一个NSMutableSet已检查的 ManagedObject(使用 CoreData 时)或仅存储已检查的 IndexPaths。在 -cellForRowAtIndexPath: 中,您可以检查是否应该检查单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *const identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.textLabel.textColor = UIColor.whiteColor;
    }

    if ([self.checkedIndexPaths containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *const cell = [tableView cellForRowAtIndexPath:indexPath];
    [table deselectRowAtIndexPath:indexPath animated:NO];

    if ([self.checkedIndexPaths containsObject:indexPath]) {
        [self.checkedIndexPaths removeObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else {
        [self.checkedIndexPaths addObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
于 2012-04-18T20:18:53.333 回答
0

由于正在重复使用单元格,因此您需要在 cellForRowAtInexPath 表数据源方法中为表中的每个单元格设置附件标记为开或关。

因此,应该在 cellForRowAtInexPath 中指定 cell.accessoryType 单元格属性,而不是在 didSelectRow 委托方法中。

在 didSelectRow 中,只需跟踪数组中选定的行,并根据数组值在 cellForRowAtInexPath 中将单元格辅助标记设置为 none 或复选标记。

于 2012-04-18T20:21:40.003 回答