Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - How can I get the IndexPath of the last item in a tableview?

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!

like image 343
Nick Avatar asked Apr 25 '11 03:04

Nick


People also ask

How do you get the IndexPath row when a button in a cell is tapped Objective C?

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.

What is IndexPath in Tableview Swift?

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.

What does IndexPath row return?

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.


1 Answers

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]; 
like image 81
Ryan Grimm Avatar answered Oct 02 '22 20:10

Ryan Grimm