UITableViewCell selector setSelected:animated: gets called many times?

前端 未结 4 570
野趣味
野趣味 2021-01-31 19:56

I\'ve discovered a strange behavior with setSelected:animated: in my custom UITableViewCell class. I discovered that this function gets called multiple times if

4条回答
  •  孤独总比滥情好
    2021-01-31 20:39

    This is a normal behavior if you're using iPad. (it is only called once on iPhone).

    In order to stop getting multiple "setSelected:YES" or multiple "setSelected:NO", all you have to do is this:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    Now, 1 click on any cell gives you:

    • 1 entry of setSelected:YES animated:NO
    • 1 entry of tableView: didSelectRowAtIndexPath:
    • 1 entry of setSelected:NO animated:YES

    So, calls are now stable regardless of what you do.

提交回复
热议问题