Vla*_*yov 4 objective-c uitableview uitextfield ios
我有一个拆分视图控制器:view&tableview.在此表视图中,我有自定义单元格和文本字段.我不知道我有多少个细胞,所以它会自动生成.现在我正在尝试滚动到textfield,当它成为FirstResponder时.我尝试过这样的事情:
-(void) textFieldDidBeginEditing:(UITextField *)textField {
CGPoint focusOnTextField = CGPointMake(0, 300 + textField.frame.origin.y);
[scroller setContentOffset: focusOnTextField animated: YES];
}
Run Code Online (Sandbox Code Playgroud)
300px - 我的TableView的起始位置.一切似乎都没问题,但textField.frame.origin.y总是等于0(喜欢和bounds.origin.y顺便说一句).
我以为我可以解决一个问题,如果得到的细胞,其文本框是活动的位置,然后更换textField.frame.origin.y上cell.frame.origin.y或这样的事情.
================================================== =================
我忘了说我的tableviews滚动被禁用了.我按照你的建议和代码示例解决它:
- (UITableViewCell *)cellWithSubview:(UIView *)subview {
while (subview && ![subview isKindOfClass:[UITableViewCell self]])
subview = subview.superview;
return (UITableViewCell *)subview;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UITableViewCell *activeCell = [self cellWithSubview:textField];
float offsetValueY = 200 + activeCell.origin.y;
CGPoint focusOnTextField = CGPointMake(0, offsetValueY);
[scroller setContentOffset:focusOnTextField animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
知道吗?它的工作原理!:-)但它创造了一个新问题.当我开始编辑文本字段时,滚动条首先跳到顶部,然后才转到正确的位置.当我写[scroller setContentOffset:focusOnTextField animated:NO];这个问题时,这个问题就消失了,但是滚动没有顺利的移动.这对我不好:-)那么我们如何解决呢?
以下是滚动到包含文本字段的单元格的方法...
// find the cell containing a subview. this works independently of how cells
// have been constructed.
- (UITableViewCell *)cellWithSubview:(UIView *)subview {
while (subview && ![subview isKindOfClass:[UITableViewCell self]])
subview = subview.superview;
return (UITableViewCell *)subview;
}
Run Code Online (Sandbox Code Playgroud)
您的想法在编辑开始时触发该操作是正确的.只需使用单元格...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// find the cell containing this text field
UITableViewCell *cell = [self cellWithSubview:textField];
// now scroll using that cell's index path as the target
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2558 次 |
| 最近记录: |