0

I need to uncheck cell in my tableView. Like a checkbox, once i pressed a cell it stay selected, if i pressed again the same cell it stay unselected. Is it possible?

All I found is some method for deselecting cells

[table deselectRowAtIndexPath:NSIndexPath animated:YES];

but it's not useful

upd: i don't need to check many cells, all i need to uncheck cell if a tap on selected cell, and if i tap on other(unselected) cell, selected should change state to unselected(it's already work by default in cells)

  • possible duplicate of [UITableViewCell Accessory Type Checked on Tap & Set other unchecked](http://stackoverflow.com/questions/1750753/uitableviewcell-accessory-type-checked-on-tap-set-other-unchecked) – jbouaziz Aug 18 '14 at 13:04
  • Check this answer, it should help http://stackoverflow.com/questions/1750753/uitableviewcell-accessory-type-checked-on-tap-set-other-unchecked – jbouaziz Aug 18 '14 at 13:05
  • -allowsMutipleSelection https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/allowsMultipleSelection – Andrea Aug 18 '14 at 13:06
  • @Andrea i don't need to check many cells, all i need to uncheck cell if a tap on selected cell, and if i tap on other(unselected) cell, selected should change state to unselected –  Aug 18 '14 at 13:09

3 Answers3

0

Where your datasource is load do this

arrSelected = [[NSMutableArray alloc] init];
for (int i = 0; i <arrSong.count; i++) {
    [arrSelected addObject:[NSNumber numberWithBool:NO]];
}

In your cell for indexpath do this

if ([[arrSelected objectAtIndex:indexPath.row] boolValue] == YES) {
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else{
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

To select/deselect do thisin you tableview:didselect

    if ([[arrSelected objectAtIndex:indexPath.row] boolValue] == YES) {
        [arrSelected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];
    }
    else{
        [arrSelected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
    }
BHASKAR
  • 1,201
  • 1
  • 9
  • 20
  • good, but works only if i add [tableView reloadData]; if ([[arrSelected objectAtIndex:indexPath.row] boolValue] == YES) { [arrSelected replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]]; [tableView reloadData]; } –  Aug 18 '14 at 13:42
0

the easiest way, but it's not the best: In didSelectRow, before setingTheAccesoryType you just have to call:

[tableView reloadData];

and this will deselect all the rows all the rows, and after that check the currentCell,

in cellForRow you set the accesoryType of the cell to none.

Bogdan Somlea
  • 604
  • 3
  • 15
0
ios swift

1. by default all the cell are inactive state so store cell in dictionary

var indexPathCheckmark = [Int: Bool]()

// set all the values initally false for check mark
                    for var index = 0 ; index < self.listOfOldApps.count ; index++ {
                        self.indexPathCheckmark[index] = false
                    }
  1. inside your didSelectRowAtIndexPath using following

    // set a check marker for all cell required
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        if (indexPathCheckmark[indexPath.row] == false) {
            indexPathCheckmark[indexPath.row] = true
            cell?.accessoryView = UIImageView(image: UIImage(named: "done-active"))
        } else {
            indexPathCheckmark[indexPath.row] = false
            cell?.accessoryView = UIImageView(image: UIImage(named: "done"))
        }
    
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51