How to know the UITableview row number

前端 未结 10 1843
粉色の甜心
粉色の甜心 2020-11-22 11:28

I have a UITableViewCell with UISwitch as accessoryview of each cell. When I change the value of the switch in a cell, how can I know in which row

10条回答
  •  臣服心动
    2020-11-22 11:48

    One common way to do this is to set the tag of the control (in your case the switch) to something that can be used to identify the row or represented object.

    For example, in tableView:cellForRowAtIndexPath: set the tag property of the switch to the indexPath.row and in your action method you can get the tag from the sender.

    Personally, I don't like this approach and prefer subclassing UITableViewCell. Also, it may be a good idea to add an "offset" to the tag to prevent any conflicts with the tags of other views.

提交回复
热议问题