当我在 UITableview 上滚动时,多个选定行的复选标记是 disappears.how 来解决这个问题?

When i scrolling on UITableview,the checkmark of multiple selected rows are disappears.how to solve this?

我想在 UITableview.but 中实现多行 selection 当我 select 带有复选标记的多行并向下滚动时,当我回到 [=17] 时复选标记消失=]ed 行 section.I 尝试了 Whosebug 的许多解决方案,但它不适用于 me.Can 你请任何人给我适合我的解决方案。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *simpleTableIdentifier = @"SimpleTableCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
            if([_selectedstatearray containsObject:indexPath]) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else {
                cell.accessoryType = UITableViewCellAccessoryNone;

            }


        cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
        return cell;

    }


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

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *cellText = cell.textLabel.text;
        NSLog(@"cellText>>%@",cellText);

        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            [_selectedstatearray removeObject:cellText];
            cell.accessoryType = UITableViewCellAccessoryNone;

        } else {
            [_selectedstatearray addObject:cellText];
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }

        NSLog(@"selectedarray>>%@",_selectedstatearray);

        NSString *greeting = [_selectedstatearray componentsJoinedByString:@","];
        NSLog(@"%@",greeting);


        [tableView deselectRowAtIndexPath:indexPath animated:YES];


    }

问题出在这段代码:

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

您正在检查 _selectedstatearray 是否包含指定的 indexPath,同时将它们存储为 NSString [_selectedstatearray addObject:cellText];

将上面的代码替换为:

NSString *text = [statearray objectAtIndex:indexPath.row];
if([_selectedstatearray containsObject:text]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

回答您最后的评论

如果您将 selectedstatearray 传递给另一个 ViewController's (viewControllerB) UITableView将仅包含 selectedstatearray 然后您可以添加

cell.accessoryType = UITableViewCellAccessoryCheckmark

在您的 UITableVIew 委托方法中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

这将为所有行添加复选标记

希望对您有所帮助....