简体   繁体   中英

How to get last cell index path in iOS?

I am building an IOS application where I am using tableView . Now when I reached to last cell I load +10 data from localDB.

After fetching the data I reloaded the tableView that I don't need in place of reload I want to used updated tableView . for that SOF suggested me below code.

[[self tableView] beginUpdates];

[[self tableView] reloadRowsAtIndexPaths:____ withRowAnimation:UITableViewRowAnimationAutomatic];

[[self tableView] endUpdates];

I don't what should be there in reloadRowsAtIndexPaths value. Please can some help me in understanding this above line of code.

You can get the indexPath of the last row in last section like this.

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)];

Here, numberOfSections is the value you return from numberOfSectionsInTableView: method. And, numberOfRowsInLastSection is the value you return from numberOfRowsInSection: method for the last section in the table view.

You don't need to reload cells, because there are no cells after your 10th cell. You want your users to see more cells when they get to the last one as in a news app. The solution is to inset more rows below, here is how:

1- create the rows you want to insert

NSMutableArray *newRows = @[[NSIndexPath indexPathForRow:10 inSection:0]];


3- update your data source array
2- [self.tableView insertRowsAtIndexPaths: newRows, UITableViewRowAnimationAutomatic];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM