UITableView scrollToRowAtIndexPath not working for last 4
I am in a UITableViewController and I have textfields inside cells. When the user clicks on the textfield, I implement the UITextFieldDelegate and in method textFieldDidBeganEditing I determine the index of the cell and scroll to that position. It works perfectly for all cells expect for the last 4-5 cells. What am I doing wrong? Thanks.
- How do I use Cocoapods in an embedded framework?
- How can I get a precise time, for example in milliseconds in Objective-C?
- Objective-C autorelease memory management
- How can I make my UIPickerView show labels after the selected value?
- How to always makes a UIScrollView scrollable?
- How to consolidating the translucency of the navigation bar between iPhone 5S and 5?
3 Solutions Collect From Internet About “UITableView scrollToRowAtIndexPath not working for last 4”
scrollToRowAtIndexPath: method scrolls the cell to UITableViewScrollPositionTop or UITableViewScrollPositionMiddle only if the tableView’s contentSize is large enough to bring the cell to those positions. If the cell you are trying to scroll to top or middle is the last cell or among the last few cells, it can not be scrolled to middle or top. In these situations you need to calculate the contentOffset manually for those cells.
Edit – Calculating the contentOffset:
In order to calculate the contentOffset use the method as specified by @Schoob. Put the following code in your textFieldDidBeginEditing method.
CGPoint origin = textField.frame.origin; CGPoint point = [textField.superview convertPoint:origin toView:self.tableView]; float navBarHeight = self.navigationController.navigationBar.frame.size.height; CGPoint offset = tableView.contentOffset; // Adjust the below value as you need offset.y += (point.y - navBarHeight); [tableView setContentOffset:o animated:YES];
I found the key to using scrollToRowAtIndexPath was to make sure to call this after the view has been presented. I.e. pushed on to a navigationcontroller or presented modally.
My working code goes like this
MyViewController *cont = [[MyViewController alloc] initWithMedication:medication]; cont.sectionToShow = 1; [self.navigationController pushViewController:cont animated:YES]; [cont release];
Viewcontroller code inside viewWillAppear
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.sectionToShow]; [cont.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
If I did it any other way there were always edge cases where it didn’t work. I guess we want to scroll late in the presentation cycle.
This is what you should be using when trying to determine the indexPath of a view/textField added on a UITableViewCell
CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
Then use point with
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
- issue while changing the CurrentPage of UIPageControl which is Inside a TableViewCell
- numberOfRowsInSection is called before Alamofire connection
- email from Apple – “Missing Push Notification Entitlement”
- Objective C: How to check if application is currently active (i.e. user is using it)?
- Call method when Button leaves Highlighted State
- How to Pass Data from View Controllers?
- Swift – How creating custom viewForHeaderInSection, Using a XIB file?
- UICollectionView is not starting at the Y position 0 in iOS
- How to impliment iOS In App Purchase for books with dynamic price?
- How can I test my iPad app on my actual iPad?
- ios AVCaptureVideoPreviewLayer capture current image
- iOS7 issues with NSIBPrototypingLayoutConstraint autolayout constraints generated by Interface Builder
- Objective c checking whether text field is empty
- Any advantage of programming natively for mobile development?
- In Xcode, how can you check environment variables to know which build configuration or scheme you have selected?