How to get UITableViewCell indexPath from the Cell?

后端 未结 11 1622
野趣味
野趣味 2020-12-07 20:11

How do I, from a cell, get its indexPath in a UITableView?

I\'ve searched around stack overflow and google, but all the information is on t

11条回答
  •  旧巷少年郎
    2020-12-07 20:39

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    

    It helps reading the UITableView documentation, even if this is by some regarded to be controversial (see comments below).

    The cell has no business knowing what its index path is. The controller should contain any code that manipulates UI elements based on the data model or that modifies data based on UI interactions. (See MVC.)

提交回复
热议问题