将UITableViewCell滚动到可见不按预期工作

Sim*_*iwi 6 scroll uitableview ios

我在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];
    }
Run Code Online (Sandbox Code Playgroud)

谁有人在这看到我做错了什么?

wj2*_*061 5

UITableViewCell被重用,保留UITableViewCell的subView的引用不是一个好方法.
如果特殊的UITableViewCell不在UITableView中visibleCells,则其框架未定义.
解决方案是:

  1. 创建一个自定义UITableViewCell,它具有您需要的相同结构.

  2. 保留自定义UITableViewCell的索引路径的引用.

  3. 使用此索引路径来完成工作.

我希望这会奏效:

[measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];    
CGPoint offset = measurementTableView.contentOffset;     
offset.y += backgroundPickerView.frame.height;
Run Code Online (Sandbox Code Playgroud)


Sim*_*iwi 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];
}
Run Code Online (Sandbox Code Playgroud)