tableView convertPoint:fromView:奇怪的行为和解决方法

我有一个自定义单元格的tableView。 单元格中的一个对象是一个textField。 tableViewController是textFieldDelegate。 该表的数据源是一个对象数组。 当用户点击textField时,tableViewController实现了textField委托方法,控制这种方式的数据input。
要使用用户input的数据,tableViewController实现以下内容:

- (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总是错误的,它总是行+ 1它假设是。 解决scheme:在故事板中,我将textField移动到单元格的上半部分。 有没有人看到过这样的事情? 谢谢。

-[UIView center]返回视图框架的中心,该框架位于视图的超视图坐标系中。 但是-[UIView convertPoint:fromView:]需要在“from”视图的坐标系中给出一个点。

要么给它同样的观点,并告诉它从正确的观点转换:

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

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

 CGRect textFieldBounds = textField.bounds; CGPoint point = CGPointMake(CGRectGetMidX(textFieldBounds), CGRectGetMidY(textFieldBounds)); point = [self.tableView convertPoint:point fromView:textField]; 
  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *text = [array_loaddata objectAtIndex:indexPath.row] ; UIFont *font = [UIFont systemFontOfSize:10]; CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping]; int count = size.height + 0; return count; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [array_loaddata count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier =[NSString stringWithFormat:@"Cell%d",indexPath.row] ; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; NSString *text = [array_loaddata objectAtIndex:indexPath.row] ; UIFont *font = [UIFont systemFontOfSize:10]; CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(46, 0, size.width, size.height)]; label.numberOfLines = 0; label.textColor = [UIColor grayColor]; label.lineBreakMode = NSLineBreakByWordWrapping; label.text = (text ? text : @""); label.font = font; label.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview:label]; [label release]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { link==http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/ /* NSString *appurl =[NSString stringWithFormat:@"xx"]; appurl = [appurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:appurl]]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; */ }