我在UITableViewCell中有一个按钮,它存储为名为currentButton的ivar。单击该按钮时,将调用包含UIPickerView和UIToolBar的UIView,并从屏幕底部显示。但是,我已经看了几个小时的其他帖子,以下代码仍然无法正常工作。单元格有时会在键盘下方向下滚动,UITableView底部的单元格不会向上滚动到UIView backgroundPickerView的顶部。这是我的代码:
CGPoint center = currentButton.center;
CGPoint rootViewPoint = [currentButton.superview convertPoint:center toView:self.view];
NSIndexPath *indexPath = [measurementTableView indexPathForRowAtPoint:rootViewPoint];
CGRect cellRect = [measurementTableView rectForRowAtIndexPath:indexPath];
if (cellRect.origin.y + cellRect.size.height >= backgroundPickerView.frame.origin.y) {
[measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
CGPoint offset = measurementTableView.contentOffset;
offset.y += ((cellRect.origin.y - cellRect.size.height) - backgroundPickerView.frame.origin.y);
[measurementTableView setContentOffset:offset animated:YES];
}
有谁看到我在这里做错了什么?
答案 0 :(得分:5)
UITableViewCell被重用,保留UITableViewCell子视图的引用不是一个好方法。
如果特殊的UITableViewCell不在UITableView' visibleCells
中,则其框架未定义。
解决方案是:
创建一个自定义UITableViewCell,它具有您需要的相同结构。
保留自定义UITableViewCell的索引路径的引用。
我希望这会奏效:
[measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
CGPoint offset = measurementTableView.contentOffset;
offset.y += backgroundPickerView.frame.height;
答案 1 :(得分:5)
这段代码对我有用。使用与此处建议的方法不同的方法,它对我需要的东西很有用。
CGRect buttonFrame = [currentButton convertRect:currentButton.bounds toView:measurementTableView];
NSIndexPath *indexPath = [measurementTableView indexPathForRowAtPoint:buttonFrame.origin];
CGRect backPickFrame = [backgroundPickerView convertRect:backgroundPickerView.bounds toView:measurementTableView];
if (buttonFrame.origin.y + buttonFrame.size.height >= backPickFrame.origin.y) {
measurementTableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, backgroundPickerView.frame.size.height, 0.0);
[measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
} else {
[measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
答案 2 :(得分:3)
第一个问题是UITableView部分隐藏在键盘下。因此,当您尝试滚动到底部行时,表格将会反弹并且所需内容不可见。
要在出现/隐藏键盘时解决此问题,您有两种选择。
self.tableView.contentInstet.bottom
的值以考虑键盘大小完成后,您可以这样做:
CGRect rectToBeVisible = [currentButton convertRect: currentButton.bounds
toView: self.tableView];
[self.tableView scrollRectToVisible: rectToBeVisible animated: YES];
我可能搞砸了应该执行转换的视图,所以试验一下,但一般概念应该是明确的。