The code below correctly returns the cell:
func findSuperView(sender:UIButton!) -> UITableViewCell {
var superView : UIView? = sender.superview
var foundSuperView : UITableViewCell!
while superView != nil && foundSuperView == nil {
if let cell = superView as? UITableViewCell {
foundSuperView = cell
break
}
else {
superView = superView?.superview
}
}
return foundSuperView
}
But for finding indexpath in tableview it crashes:
var indexPath : NSIndexPath = self.table .indexPathForCell(findSuperView(sender))!
println("Section (indexPath)")
And I tried another way, but it was not successful:
var button : UIButton = sender as UIButton;
var touch: UITouch = events .allTouches()?.anyObject() as UITouch
var location : CGPoint = touch.locationInView(self.table)
var indexPath : NSIndexPath = self.table.indexPathForRowAtPoint(location)!
I don't know if there is an easy a way to do this. (Edit: Actually there is. Look at @mustafa's second solution.) A workaround is to set the button's tag to indexPath.row
in cellForRowAtIndexPath
, then you can just access the button's tag to find out which row it belongs to.
Warning: This workaround is fragile. It won't work correctly if you allow rows to be added or deleted from your table without then calling tableView.reloadData()
. Look at @mustafa's solution which is much more robust.
Thanks for quick reply, i applied your solution. It works for me. But like to have a proper alternative working solution on it.
No, using a tag based on the indexPath won't work if any rows can be added or removed.
@rmaddy. Good point. I updated my answer to note that. Since the other answers weren't working for OP, I took a completely different approach in a new answer. Since my original answer was accepted by OP, I didn't want to edit it and give the impression that my new answer had been accepted.