如何知道UITableview行号 [英] How to know the UITableview row number
问题描述
我有 UITableViewCell
,其中 UISwitch
作为每个单元格的附件视图。当我更改单元格中交换机的值时,如何知道交换机在哪一行?我需要切换值更改事件中的行号。
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 the switch is? I need the row number in the switch value changed event.
推荐答案
标签,子类或视图层次结构导航也是很多工作!。在您的操作方法中执行此操作:
Tags, subclasses, or view hierarchy navigation are too much work!. Do this in your action method:
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
适用于任何类型的视图,多节表,无论你能投入什么 - 只要您的发件人的来源是在单元格的框架内(感谢抢劫!),通常情况就是如此。
Works with any type of view, multi section tables, whatever you can throw at it - as long as the origin of your sender is within the cell's frame (thanks rob!), which will usually be the case.
这里是UITableView Swift扩展名:
And here it is in a UITableView Swift extension:
extension UITableView {
func indexPath(for view: UIView) -> IndexPath? {
let location = view.convert(CGPoint.zero, to: self)
return self.indexPathForRow(at: location)
}
}
这篇关于如何知道UITableview行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!