You are just assuming that the cell's immediate superview is the table view - wrongly. There is no particular reason why that should be so (and indeed it is not). Work with fewer assumptions! You need to keep walking up the superview chain until you do reach the table, like this:
var v : UIView = self
do { v = v.superview! } while !(v is UITableView)
Now v
is the table view, and you can proceed to work out what row this is.
What I would actually do, however, is work my up, not from the cell to the table, but from the button to the cell. The technique is exactly the same:
var v : UIView = sender as! UIView
do { v = v.superview! } while !(v is UITableViewCell)
Do that the button's action method, where sender
is the button. If the target of the action method is the table view controller, it has access to the table, and the problem is solved.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…