2013-07-09 34 views
1

我有一个动态tableView。几个单元格有一个textFieldUITableView在textFieldDidBeginEditing中获取indexPath:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15,10,260,40)]; 
textField.delegate = self; 
[cell addSubview:textField]; 

我试图让textFieldDidBeginEditingindexPath。这里是我的代码

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    CGPoint location = [self.view.superview convertPoint:self.view.center toView:self.orderTableView]; 
    NSIndexPath *indexPath = [self.orderTableView indexPathForRowAtPoint:location]; 
    NSLog (@"IndexPath %@", indexPath); 
    NSLog (@"IndexPath.ROW %i", indexPath.row); 
} 

我有第一个4个单元格(索引0 - 3)是标准单元格。在前4个单元之后,我可以动态地使用UITextField将多达3个自定义单元。

的UIViewController中后,会出现与UITableView的,而不管是什么TableViewCell用的UITextField我点击的的NSLog下面不断显示我下面的:

2013-07-09 07:21:40.910 MyApp[2884:c07] IndexPath <NSIndexPath 0xa0a5d60> 2 indexes [0, 3] 
2013-07-09 07:21:40.910 Mypp[2884:c07] IndexPath.ROW 3 

我如何才能让细胞与正确indexPathUITextField那叫做键盘?

回答

4

我认为你是在正确的轨道上。但我相信你引用的观点(视图控制器视图的中心)不会给你一个准确的位置。只要你的文本框的框架保持在单元格内,我相信这应该起作用。

//convert the textfield's frame origin in the cell to frame origin in table view 
CGPoint location = [self.tableView convertPoint:textField.frame.origin fromView:textField.superview]; 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
+0

cool :) ..正确答案 –

相关问题