我用一些自定义单元格组成了一个表格 View 。一些自定义单元格包含 UIWebView
,一些包含 UISlider
加 UITextField
,一些包含 UIButton
。
现在,当我单击 UITableView
底部的 uitableviewcell subview 文本字段时,我无法使我的表格单元格在键盘上方正确可见。
使用 UITableViewController
而不是 UIViewController
对我来说是不可能的。因为我的 UI 结构有点奇怪而且动态。简单来说,就像
UIView -> UIScrollView with paging -> x number of UITableViews -> y number of UITableViewCell -> z number of UITextFields, UISliders, UIWebViews, UIButtons, UILables, etc.
我还尝试了下面的代码以使其工作。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
但它不起作用:(
谢谢
编辑1:
我已经检查了文本字段、tableviewcell 和 tableview 的引用。所以对象的引用不是问题。
最佳答案
试试这个代码......
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
NSIndexPath *indexPath = [[table_Question[pageNumber] indexPathForCell:cell];
int totalRows = 0;
if([[table_Question[pageNumber] numberOfSections] > 1)
{
for(int i = 0; i<([indexPath.section] - 1);i++)
{
totalRows += [[table_Question[pageNumber] numberOfRowsInSection:i];
}
}
totalRows += indexPath.row;
int yPosition = totalRows * cell.frame.size.height;
CGPoint offset = CGPointMake(0, yPosition);
[[table_Question[pageNumber] setContentOffset:offset animated:YES];
}
这会对你有帮助...
关于ios - UITableView 与 UITextView 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15680020/