tableView convertPoint:fromView:奇怪的行为和变通方法 [英] tableView convertPoint: fromView: odd behaviour and workaround

查看:126
本文介绍了tableView convertPoint:fromView:奇怪的行为和变通方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带自定义单元格的tableView。单元格中的一个对象是textField。
tableViewController是textFieldDelegate。
表的数据源是一个对象数组。
当用户点击textField时,tableViewController实现textField委托控制这种方式数据条目的方法。

要使用用户输入的数据,tableViewController实现以下内容:

I have a tableView with custom cells. One of the objects in the cell is a textField. The tableViewController is the textFieldDelegate. The datasource for the table is an array of objects. When the user tap on the textField, the tableViewController implements textField delegates methods controlling this way the data entry.
To use the data entered by user, the tableViewController implements the following:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
  {
    CGPoint point = [textField center];
    point = [self.tableView convertPoint:point fromView:textField];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
... 
...
    return YES
  }

我花了两个小时试图找出为什么indexPath.row总是错误的,它总是排在第一行的假设。
解决方案:在故事板中,我将textField移动到单元格的上部。
有没有人看过这样的东西?
谢谢。

I spent two hours trying to figure out why indexPath.row was always wrong, it was always row+1 of what it suppose to be. Solution: In the storyboard I move the textField to the upper portion of the cell. Has anyone see something like this before? Thanks.

推荐答案

- [UIView center] 返回视图的框架的中心,该框架位于视图的 superview的坐标系中。但 - [UIView convertPoint:fromView:] 期望在from视图的坐标系中给出一个点。

-[UIView center] returns the center of the view's frame, which is in the view's superview's coordinate system. But -[UIView convertPoint:fromView:] expects to be given a point in the "from" view's coordinate system.

要么给它相同的点,并告诉它从正确的视图转换:

Either give it the same point, and tell it to convert from the correct view:

CGPoint point = [textField center];
point = [self.tableView convertPoint:point fromView:textField.superview];

或者在视图的坐标系中给它一个点,并告诉它从视图转换:

Or give it a point in the view's coordinate system, and tell it to convert from the view:

CGRect textFieldBounds = textField.bounds;
CGPoint point = CGPointMake(CGRectGetMidX(textFieldBounds), CGRectGetMidY(textFieldBounds));
point = [self.tableView convertPoint:point fromView:textField];

这篇关于tableView convertPoint:fromView:奇怪的行为和变通方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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