简体   繁体   English

如何获取iOS中的最后一个单元格索引路径?

[英]How to get last cell index path in iOS?

I am building an IOS application where I am using tableView . 我正在使用tableView构建一个IOS应用程序。 Now when I reached to last cell I load +10 data from localDB. 现在,当我到达最后一个cell我从localDB加载了+10数据。

After fetching the data I reloaded the tableView that I don't need in place of reload I want to used updated tableView . 提取数据后,我重新加载了不需要重新加载的tableView ,我想使用更新的tableView for that SOF suggested me below code. 对于那个SOF建议我下面的代码。

[[self tableView] beginUpdates];

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

[[self tableView] endUpdates];

I don't what should be there in reloadRowsAtIndexPaths value. 我在reloadRowsAtIndexPaths值中不应该有什么。 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. 您可以像这样在最后一节中获取最后一行的indexPath。

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

Here, numberOfSections is the value you return from numberOfSectionsInTableView: method. 在这里,numberOfSections是您从numberOfSectionsInTableView:方法返回的值。 And, numberOfRowsInLastSection is the value you return from numberOfRowsInSection: method for the last section in the table view. 并且,numberOfRowsInLastSection是您从numberOfRowsInSection:方法返回的表视图最后一部分的值。

You don't need to reload cells, because there are no cells after your 10th cell. 您不需要重新加载单元格,因为第10个单元格之后没有单元格。 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 1-创建要插入的行

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


3- update your data source array 3-更新您的数据源阵列
2- [self.tableView insertRowsAtIndexPaths: newRows, UITableViewRowAnimationAutomatic]; 2- [self.tableView insertRowsAtIndexPaths: newRows, UITableViewRowAnimationAutomatic];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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