5

我制作了一个 tableView,我需要在其中选择多个选项。选项被选中,但是当我滚动表格视图时,复选标记选项消失,其他一些行显示该复选标记。这是我在didselectedrowAtindex方法中的代码table_optionUITableView并且selectedcellsNSMutableArray

[table_option deselectRowAtIndexPath:indexPath animated:YES];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum]  )
{
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
        cell.accessoryType = UITableViewCellAccessoryNone;
    else 
        cell.accessoryType = UITableViewCellAccessoryCheckmark;



    [selectedCells removeObject:rowNsNum];
    sel.text=@"Select";
   // cell.accessoryType = UITableViewCellAccessoryNone;

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

    [selectedCells addObject:rowNsNum];
   sel.text=@"Selected";
   // cell.accessoryType = UITableViewCellAccessoryCheckmark;

}



[table_option reloadData];

请尽快帮助

4

3 回答 3

12

您需要检查方法中是否已选择单元格cellForRowAtIndexPath。发生此问题是因为 tableView 重复使用了单元格。请在您的方法中编写以下代码cellForRowAtIndexPath,它将解决问题。

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum]  )
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
于 2012-10-23T08:39:36.763 回答
0

就我而言,只是设置accessoryType = UITableViewCellAccessoryCheckmark不起作用cellForRowAtIndexPath

UITableView到达第一个元素时会出现问题,如果您尝试滚动更多,可见单元格会下降并且它们的复选标记会消失。

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];

[cell setAccessoryType:UITableViewCellAccessoryNone];// tricky part is this line

if ( [selectedCells containsObject:rowNsNum]  )
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
于 2014-05-30T13:47:20.020 回答
0

检查这个例子

cell.accessoryType = UITableViewCellAccessoryNone;
 for (int x = 0; x < selectedIds.count; x++) {
     if ([[selectedIds objectAtIndex:x] isEqualToString:[[source objectAtIndex:[indexPath row]] objectForKey:@"id"]])
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
 }
于 2014-12-09T17:49:57.713 回答