如何知道 UITableview 的行号 [英] How to know the UITableview row number

查看:28
本文介绍了如何知道 UITableview 的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewCellUISwitch 作为每个单元格的附件视图.当我更改单元格中开关的值时,如何知道开关在哪一行?我需要开关值更改事件中的行号.

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];

适用于任何类型的视图、多节表,无论你可以扔什么——只要你的发件人的原点在单元格的框架内(感谢 rob!),这通常是这种情况.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆