选择文本字段时,使UITableView滚动 [英] Making a UITableView scroll when text field is selected
问题描述
经过大量的反复试验后,我放弃并提出问题。我见过很多人有类似的问题,但无法得到所有正确的答案。
After a lot of trial and error, I'm giving up and asking the question. I've seen a lot of people with similar problems but can't get all the answers to work right.
我有一个 UITableView
由自定义单元格组成。单元格由5个文本字段组成,彼此相邻(有点像网格)。
I have a UITableView
which is composed of custom cells. The cells are made of 5 text fields next to each other (sort of like a grid).
当我尝试滚动并编辑<底部的单元格时code> UITableView ,我无法让我的单元格正确定位在键盘上方。
When I try to scroll and edit the cells at the bottom of the UITableView
, I can't manage to get my cells properly positioned above the keyboard.
我看到很多答案在说话关于改变视图大小等...但是到目前为止它们都没有很好地工作。
I have seen many answers talking about changing view sizes,etc... but none of them has worked nicely so far.
任何人都可以通过具体的代码示例澄清正确的方法?
Could anybody clarify the "right" way to do this with a concrete code example?
推荐答案
滚动功能可以简单得多:
The function that does the scrolling could be much simpler:
- (void) textFieldDidBeginEditing:(UITextField *)textField {
UITableViewCell *cell;
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
cell = (UITableViewCell *) textField.superview.superview;
} else {
// Load resources for iOS 7 or later
cell = (UITableViewCell *) textField.superview.superview.superview;
// TextField -> UITableVieCellContentView -> (in iOS 7!)ScrollView -> Cell!
}
[tView scrollToRowAtIndexPath:[tView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
就是这样。根本没有计算。
That's it. No calculations at all.
这篇关于选择文本字段时,使UITableView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!