将自定义UITableViewCell滚动到键盘右上方?
目前我有一个UITableView与自定义单元格,并在每个单元格中有一个UITextField,问题是有时UITextField由UIKeyboard覆盖。将自定义UITableViewCell滚动到键盘右上方?
所以现在我有了UIKeyboard的Y坐标,并且我的UITableView在单元格中正常工作。
所以我几乎可以使用Y坐标(浮点数),以便将我的UITableView滚动到该Y坐标加上单元格的高度,以便在UIKeyboard上方正确显示它?
另外,当我的键盘隐藏,我将如何重置UITableView到它的正常位置?
任何意见将不胜感激!
谢谢!
CustomCell *cell = (CustomCell*)[[thetextField superview] superview];
CGRect cellRect = [cell convertRect:cell.frame toView:self.view];
float bottomCell = cellRect.origin.y - cellRect.size.height;
if (bottomCell >= keyboardY) {
NSIndexPath *indexPath = [thetableView indexPathForCell:cell];
[thetableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
一方或另一方UITableView
的
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
我猜,控制不进入if语句的。考虑跳过y坐标检查而只是滚动表视图而不管它在哪里。
CustomCell *cell = (CustomCell *)[[theTextField superview] superview];
NSIndexPath *indexPath = [theTableView indexPathForCell:cell];
[theTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
OK,如果你确定这是越来越调用,然后确保你的indexPath不为零,如果电池不在的tableView或者如果它不是一个细胞可能发生。
我试了第一个,我有索引路径,我做UITableViewScrollPositionTop,它不滚动任何地方 – 2012-02-28 02:54:55
请发布您的代码。 – QED 2012-02-28 03:00:06
我发布了上面:) P.S - 如果语句被调用(用NSLogs确认) – 2012-02-28 03:03:19
只要显示键盘,UITableView就会自行调整。
但是,如果您未将自定义单元格的UITableViewCellSelectionStyle
设置为UITableViewCellSelectionStyleNone
,则不会这样做。
我通过使用下面的代码解决。
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
'scrollToRowAtIndexPath'方法知道你的tableView的可见部分,所以你不需要if语句。 – inorganik 2012-09-17 19:36:05