单击UITableViewCell中的UITextField [英] Clicking on UITextField in a UITableViewCell

查看:119
本文介绍了单击UITableViewCell中的UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当在 UITableViewCell 中点击 textField 时,方法 tableView:didSelectRowAtIndexPath:未被调用。问题是,我需要将 tableView 滚动到正确的位置,否则键盘会在第一个响应者上方。



<然后我必须移动这样的代码:

  [[self tableView] scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES] ; 

进入我的 tableView 委托方法和我的 UITextField 委托方法, textFieldDidBeginEditing:



是创建一个新方法的最佳方法是,将单击的单元格/文本字段的indexPath传递给它,并从tableView委托和 UITextField 委托调用该方法?更好的方法吗?

解决方案

我发现以下效果很好(假设你在表视图控制器中)

   - (void)textFieldDidBeginEditing:(UITextField *)textField {
CGPoint pnt = [self.tableView convertPoint:textField .bounds.origin fromView:textField];
NSIndexPath * path = [self.tableView indexPathForRowAtPoint:pnt];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


I have an issue where when a textField is clicked on in a UITableViewCell, the method tableView:didSelectRowAtIndexPath: does not get invoked. The problem is, I need to scroll my tableView into proper position, otherwise the keyboard goes right over the first responder.

I have to then move code like this:

[[self tableView] scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

into both my tableView delegate method and in my UITextField delegate method, textFieldDidBeginEditing:.

Is the best way to just create a new method, pass to it the indexPath of the cell/textfield being clicked, and call the method from both the tableView delegate and the UITextField delegate? better way of going about it?

解决方案

I found the following works well (It assumes you're in a table view controller)

- (void)textFieldDidBeginEditing:(UITextField *)textField{  
    CGPoint pnt = [self.tableView convertPoint:textField.bounds.origin fromView:textField];
    NSIndexPath* path = [self.tableView indexPathForRowAtPoint:pnt];
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

这篇关于单击UITableViewCell中的UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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