Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableview accessory type disappear while scrolling

I have made a tableView in which i need to select multiple options. The options are selected but when i scroll the table view the check mark option get disappear and some other rows displays that check mark. This is my code in didselectedrowAtindex method table_option is UITableView and selectedcells is NSMutableArray

[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];

pls help soon

like image 668
wasim Avatar asked Oct 23 '12 08:10

wasim


1 Answers

You need to check the cell is already selected or not in the cellForRowAtIndexPath method. This issue is happening because tableView re-uses the cells. Please write the below code in your cellForRowAtIndexPath method, it'll solve the issue.

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum]  )
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
like image 126
Midhun MP Avatar answered Oct 05 '22 00:10

Midhun MP