2013-07-09 29 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 :) ..正確答案 –

相關問題