[英]iOS Coordinates for UITextField in UITableView
I'm trying to scroll my table view so it display the cell which the user tapped (with UITextField) on top. 我正在尝试滚动表格视图,以便它在顶部显示用户点击(带有UITextField)的单元格。 But I can't get the coordinates of the UITextField in the UITableView. 但是我无法在UITableView中获取UITextField的坐标。 The code I have now is: 我现在拥有的代码是:
CGPoint point = [[self this].tableView convertPoint:self.activeField.frame.origin fromView:nil];
NSIndexPath * indexPath = [[self this].tableView indexPathForRowAtPoint:point];
NSLog(@"R:%i, S:%i", indexPath.row, indexPath.section); //always R:0, S:0
[[self this].tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
[self this] is a method which just returns the view casted to my custom object (maybe a bad name but this is beside the point). [self this]是一种只返回投射到我的自定义对象的视图的方法(可能是一个不好的名字,但这不重要)。 So my question is: "How do I get the correct coordinates of the tapped UITextField in a UITableView." 因此,我的问题是:“如何在UITableView中获取所点击的UITextField的正确坐标。”
Any help is welcome, thanks in advance 欢迎任何帮助,在此先感谢
You should use convertPoint toView
method 您应该使用convertPoint toView
方法
so instead of 所以代替
CGPoint point = [[self this].tableView convertPoint:self.activeField.frame.origin fromView:nil];
use 采用
CGPoint point = [self.tableView convertPoint:self.activeField.frame.origin toView:self.view];
Also instead of querying for fields origin and getting indexPath for that point. 另外,也不必查询字段原点并获取该点的indexPath。 You can subclass UITextField
and add a custom NSIndexPath
property.. which you can easily assign and later retrieve 您可以UITextField
并添加自定义NSIndexPath
属性..您可以轻松分配该属性,以后再检索
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.