Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didDeselectRowAtIndexPath isn't called

I've implemented thedidDeselectRowAtIndexPath method in a UITableView which includes multiple selection.

For some reason, didDeselectRowAtIndexPath is not being called by the delegate. Any suggestions? (I haven't accidentally misspelled didSelectRowAtIndexPath).

Thanks!

like image 755
yoavg1 Avatar asked Jun 25 '26 16:06

yoavg1


2 Answers

Some things to note about cell selection:

  • didDeselectRowAtIndexPath is only called in a single-selection UITableView if the user clicks a different cell than the one already selected.
  • didSelectRowAtIndexPath is called to select the new row, and didDeselectRowAtIndexPath is called to deselect the previous row
  • In a UITableView with multiple-selection, the previous row is not being deselected, so your deselect is handled in didSelectRowAtIndexPath

You can grab the cell using the delegate method you've implemented and modify it after checking it's selection like so:

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

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.selected) {
        // ... Uncheck
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}
like image 90
Alex Koshy Avatar answered Jun 28 '26 04:06

Alex Koshy


I currently use a tableView with multiple selection, and didDeselectRowAtIndexPath method is calling for me (unlike what is stated in third bullet point in answer above).

I did note that cells that were "preselected", did not call didDeselectRowAtIndexPath correctly unless I did three things:

  1. In cellForRowAtIndexPath, when I find a cell I want to preselect (you might check for matching array entries, or a string like I did), set cell.selected = true for that cell
  2. In cellForRowAtIndexPath, also make a call to method selectRowAtIndexPath for the cell to preselect
  3. In Interface Builder, uncheck the "Show Selection on Touch" checkmark for the tableView. Oddly enough, this was required to ensure that when a preselected item was tapped (ie should be deselected), that didDeselectRowAtIndexPath was called correctly. enter image description here

Example code:

if productsReceived!.rangeOfString(cell.productName.text!) != nil {
    cell.accessoryType = .Checkmark 
    tableView.selectRowAtIndexPath(indexPath, animated: true,   scrollPosition: UITableViewScrollPosition.None)
    cell.selected = true
}

I hope this helps someone who is trying to implement multiple selection, with preselected cells.

like image 35
instAustralia Avatar answered Jun 28 '26 06:06

instAustralia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!