How to get the indexpath.row when an element is activated?

前端 未结 19 2268
梦如初夏
梦如初夏 2020-11-21 23:30

I have a tableview with buttons and I want to use the indexpath.row when one of them is tapped. This is what I currently have, but it always is 0

var point =         


        
19条回答
  •  天涯浪人
    2020-11-21 23:55

    Since the sender of the event handler is the button itself, I'd use the button's tag property to store the index, initialized in cellForRowAtIndexPath.

    But with a little more work I'd do in a completely different way. If you are using a custom cell, this is how I would approach the problem:

    • add an 'indexPath` property to the custom table cell
    • initialize it in cellForRowAtIndexPath
    • move the tap handler from the view controller to the cell implementation
    • use the delegation pattern to notify the view controller about the tap event, passing the index path

提交回复
热议问题