如何在uitextfield中获取单元格的indexpath委托方法?

问题描述:

我有一个自定义单元格中的两个文本框如何获得TableView单元格的文本字段委托方法的indexpath值我想从用户获取输入值并将其保存到relavent对象。用户可以通过点击按钮,在细胞(添加更多)提前加入更多的细胞..如何在uitextfield中获取单元格的indexpath委托方法?

enter image description here

谢谢...

+0

[将参数传递给选择器]的可能重复(http://*.com/questions/12297511/pass-an-argument-to-selector) – 2012-09-07 13:30:00

更新到iOS7!

随着iOS7新功能,现在代码应该是:

UITableViewCell *textFieldRowCell; 

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { 
    // Load resources for iOS 6.1 or earlier 
    textFieldRowCell = (UITableViewCell *) textField.superview.superview; 

} else { 
    // Load resources for iOS 7 or later 
    textFieldRowCell = (UITableViewCell *) textField.superview.superview.superview; 
    // TextField -> UITableVieCellContentView -> (in iOS 7!)ScrollView -> Whoola! 
} 

NSIndexPath *indexPath = [self.tableView indexPathForCell:textFieldRowCell]; 

集小区indexpath值UITextFieldtag属性,你可以访问indexpath代理方法如textfield.tag

+0

但我有两个文本字段,如果它的一个我可以使用你说 – Deepak

+0

这两个文本字段都将在标记中具有唯一的索引路径。然后你可以检查占位符来确定哪个是url,哪个是名字textfield.like这个NSLog(@“%@”,self.textField.placeholder);如果([self.textField.placeholder isEqualToString:@“Enter name”]) – prashant

+0

我认为你想让我知道,如果你仍然挂着 – prashant

要获取indexPath,请尝试以下代码。

UIView *contentView = (UIVIew *)[textfield superview]; 
    UITableViewCell *cell = (UITableViewCell *)[contentView superview]; 
    if(IS_IOS7_OR_GREATER) { 
     cell = (UITableViewCell *)[[contentView superview] superview]; 
    } 
    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell]; 

Tats it it you done。

简单,

NSIndexPath *indexPath = [self.tableview indexPathForCell:(UITableViewCell *)[(UIVIew *)[textfield superview] superview]]; 

if(IS_IOS7_OR_GREATER) { 
    cell = (UITableViewCell *)[[[textfield superview] superview] superview]; 
} 

检查更新的答案。

+0

感谢它的工作:) – Deepak

+0

我对这种情况有一个疑问 – Siva

+0

是的,什么你的疑问是?你能把它作为一个问题发布吗? –

一个更动态的解决方案(没有硬编码上海华盈水平和不同的IOS版本相同的代码)。

此外,如果单元格不可见,则indexPathForCell:将不起作用,因此我使用indexPathForRowAtPoint:作为解决方法。

//find the UITableViewcell superview 
UIView *cell = textField; 
while (cell && ![cell isKindOfClass:[UITableViewCell class]]) 
    cell = cell.superview; 

//use the UITableViewcell superview to get the NSIndexPath 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center]; 

您可以设置文本框的标签的cellForRowAtIndexPath:这样它存储两种细胞和文本字段

例如的信息:如果它是细胞在第4行,第1和第2个文本框的标签可以分别是41和42。同样,第5行的文本字段标签应为51和52,依此类推......

然后在textfield委托方法中,您可以获取textfield.tag来标识活动文本字段。

+0

最简单的方法总是最好的! – Md1079

这就是我一直在做的事情,并且一直有更好的运气。我抓住了textField框架的原点。将其转换为一个点。然后将该点转换为索引路径。

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    CGPoint origin = textField.frame.origin; 
    CGPoint point = [textField.superview convertPoint:origin toView:self.tableView]; 

    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point]; 

    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

} 
+0

Awsome解决方案.. – svmrajesh

+1

这应该是正确的答案。 – wtoh

这可以在任何实例中的Objective-C运行来完成(不必是的UITextField),与任何相关联的对象(不必须是NSIndexPath)。

对于这个问题,我们可以创建一个类别UIView+RepresentingIndexPath

我们的界面允许我们设置和检索的NSIndexPath:

@interface UIView (RepresentingIndexPath) 
- (void)representIndexPath:(NSIndexPath *)indexPath; 
- (NSIndexPath *)representedIndexPath; 
@end 

我们实现使用Objective-C的相关对象来设置和检索一个视图索引路径:

#import "UIView+RepresentingIndexPath.h" 
#import <objc/runtime.h> 

static char IndexPathKey; 

@implementation UIView (RepresentingIndexPath) 

- (void)representIndexPath:(NSIndexPath *)indexPath 
{ 
    objc_setAssociatedObject(self, &IndexPathKey, indexPath, OBJC_ASSOCIATION_COPY_NONATOMIC); 
} 

- (NSIndexPath *)representedIndexPath 
{ 
    return objc_getAssociatedObject(self, &IndexPathKey); 
} 

@end 

在行动:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell" forIndexPath:indexPath]; 
    [cell.textField addTarget:self action:@selector(textFieldTextChanged:) forControlEvents:UIControlEventEditingChanged]; 
    [cell.textField representIndexPath:indexPath]; 
    return cell; 
} 

- (void)textFieldTextChanged:(UITextField *)sender 
{ 
    NSIndexPath *indexPath = [sender representedIndexPath]; 
    NSLog(@"%@", indexPath); 
} 

最后一个注意!如果你可以在不这样做的情况下实现你想要做的事情,那么在运行时混淆就应该避免。只是想我会添加另一种解决方案!

试试这个方法来获取文本框从tableview控制器

#pragma mark - Get textfield indexpath 
- (NSIndexPath *)TextFieldIndexpath:(UITextField *)textField 
{ 
    CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:self.TblView]; 
    NSIndexPath * indexPath = [self.TblView indexPathForRowAtPoint:point]; 
    NSLog(@"Indexpath = %@", indexPath); 
    return indexPath; 
} 

动态任何地方,我觉得这个答案搜索我怎样才能找到一个细胞具有的UITextField内索引路径。

所以,感谢上面的答案,我把我的代码放在这里,希望可能是有用的。

- (void)searchSelectedIndexPath:(UIView*)view { 
    // This allow to find selected index path for a table view cell with a text field inside. 
    for (UIView* subview in view.subviews) { 
     if ([view isKindOfClass:[UITextField class]]) { 
      if ([view isFirstResponder]) { 
       UIView *cell = view; 
       while (cell && ![cell isKindOfClass:[UITableViewCell class]]) { 
        cell = cell.superview; 
       } 
       self.selectedIndexPath = [self.tableView indexPathForRowAtPoint:cell.center]; 
       return; 
      } 
     } 
     [self searchSelectedIndexPath:subview]; 
    } 
} 

这样,当键盘通知将加薪:

- (void)keyboardDidShow:(NSNotification*)notification { 
    [self searchSelectedIndexPath:self.tableView]; 
} 

如果有人像我一样需要@Kerkness的回答(这个人真的为我工作)在SWIFT:

func textFieldDidBeginEditing(_ textField: UITextField) { 
    let origin: CGPoint = textField.frame.origin 
    let point: CGPoint? = textField.superview?.convert(origin, to: tableView) 
    let indexPath: IndexPath? = tableView.indexPathForRow(at: point ?? CGPoint.zero) 
    tableView.scrollToRow(at: indexPath!, at: .middle, animated: true) 
} 

它应该足够简单:您明白了,然后您就可以获得indexPath并根据需要做任何事情!