Scroll UITableViewCell to visible, not working as intended

I have a button in a UITableViewCell that is stored as ivar named currentButton. When this button is pressed, a UIView containing a UIPickerView and UIToolBar is called up and displayed at the bottom of the screen. However, I have been looking at other posts for several hours and this code still does not work. Cells sometimes scroll down below the keyboard, and the cells located at the bottom of the UITableView don't scroll enough to jump to 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'm doing wrong here?

+6
source share
3 answers

This code worked for me. It takes a different approach than those suggested here, and this is good for what I need.

 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]; } 
+5
source

UITableViewCell is reused, so linking to UITableViewCell is not a good approach. If the special UITableViewCell is not present in the UITableView visibleCells , its frame is undefined.
Decision:

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

  • Keep a reference to the index path for the custom UITableViewCell.

  • Use this index path to do this work.

I hope this works:

 [measurementTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; CGPoint offset = measurementTableView.contentOffset; offset.y += backgroundPickerView.frame.height; 
+5
source

The first problem is that the UITableView is partially hidden under the keyboard. Therefore, when you try to scroll to the bottom row, the table will go back and the necessary content will not be visible.

To solve this problem, when the keyboard appears / hides, you have two options.

  • self.tableView.contentInstet.bottom update self.tableView.contentInstet.bottom to take into account keyboard size
  • refresh the table frame, if you use auto-layout, then the best approach would be to update the constant part of the lower constraint (IMO is the best approach).

After that you can do this:

 CGRect rectToBeVisible = [currentButton convertRect: currentButton.bounds toView: self.tableView]; [self.tableView scrollRectToVisible: rectToBeVisible animated: YES]; 

I could ruin the view to which you want to convert, so experiment a little, but the general concept should be clear.

+3
source

All Articles