I would like to automatically scroll to the end of a table view.
[tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
Given that I know how many item are in the tableview using:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
How can I get the IndexPath * to the last item in this tableView? This is needed so that I can provide it as an argument to scrollToRowAtIndexPath:atScrollPosition:animated
Thanks!
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.
row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.
To get a reference to the last row in the last section…
// First figure out how many sections there are NSInteger lastSectionIndex = [tableView numberOfSections] - 1; // Then grab the number of rows in the last section NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1; // Now just construct the index path NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With