iOS:Tableview多选 - AccessoryCheckmark检查可重复使用的单元格

use*_*043 2 uitableview ios

我正在使用带有节和多个选择的tableview,但是当选择一行时我有一个问题,即检查多行...我已经看到了一些其他线程,但是没有真正得到解决方案. ..

这是我的代码:

-(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];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:添加了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;
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*orr 10

我建议存储已NSMutableSet检查的ManagedObject(使用CoreData时)或只检查已检查的IndexPath.In -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;
    }
}
Run Code Online (Sandbox Code Playgroud)