iOS:Tableview 多选 - AccessoryCheckmark 检查可重用单元格 [英] iOS: Tableview multiple selection - AccessoryCheckmark checking reusable cells

查看:26
本文介绍了iOS:Tableview 多选 - AccessoryCheckmark 检查可重用单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'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

Update: Added 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;
}

推荐答案

我建议存储一个 NSMutableSet 的被检查的 ManagedObject(当使用 CoreData 时)或简单的被检查的 IndexPaths.在 -cellForRowAtIndexPath: 然后你可以检查是否应该检查单元格.

I would suggest storing an NSMutableSet of either the checked ManagedObject (when using CoreData) or simply the checked IndexPaths. In -cellForRowAtIndexPath: you can then check if the cell is supposed to be checked.

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

这篇关于iOS:Tableview 多选 - AccessoryCheckmark 检查可重用单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆