Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll UITableViewCell to visible not working as intended

I have a button in a UITableViewCell which is stored as an ivar called currentButton. When that button is clicked a UIView containing a UIPickerView and UIToolBar is invoked and shown from the bottom of the screen. However, I have looked at other posts for hours and this following code still doesn't work. The cells sometimes scroll down below the keyboard and the cells all the way at the bottom of the UITableView do not scroll up enough to go on top of the UIView backgroundPickerView. This is my code:

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];
    }

Does anyone see what I am doing wrong here?

like image 898
SimplyKiwi Avatar asked Apr 29 '16 03:04

SimplyKiwi


1 Answers

UITableViewCell are reused,keeping a reference of a UITableViewCell's subView is not a good approach.
If the special UITableViewCell is not in UITableView's visibleCells ,its frame is undefined.
A solution is:

  1. Create a custom UITableViewCell that has the same structure as you need.

  2. Keep a reference of the indexpath for the custom UITableViewCell.

  3. Use this indexpath to do the work .

I hope this will work:

[measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];    
CGPoint offset = measurementTableView.contentOffset;     
offset.y += backgroundPickerView.frame.height;
like image 151
wj2061 Avatar answered Oct 24 '22 15:10

wj2061