Deselect cell while single selection in iOS5?

Imagine a simple table view, with a few cells, single selection is enabled, that means once I tap a cell, it stays selected, ie blue when using default controls. When I tap a different cell, the previous one is deselected and the current one will be selected.

Now, to make it complete (Apple, Apple :-/...) ie. to be able to deselect the currently selected cell, we can have a simple piece of code

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath        *)indexPath
{
    UITableViewCell *touchedCell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (touchedCell.isSelected == YES)
    {
        [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
        return nil;
    }
    else
    {
        return indexPath;
    }
}

There is a catch though. This method is only available in iOS6 SDK. Could you please hint at simplest most elegant solution for iOS5 SDK where I do not have this method?


All three methods:

  • tableView:willSelectRowAtIndexPath:
  • cellForRowAtIndexPath:
  • deselectRowAtIndexPath:animated:
  • are available in iOS 2.0 and later according to the docs.

    Hope this helps!

    链接地址: http://www.djcxy.com/p/60020.html

    上一篇: 难以发布行动到时间表

    下一篇: 在iOS5中单选时取消选择单元格?