如何使用调用 API 在 UITableView 中进行分页(加载更多数据,如 facebook)?法典:-
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat height = scrollView.frame.size.height;
CGFloat contentYoffset = scrollView.contentOffset.y;
CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;
if(distanceFromBottom < height)
{
[self fetchNewsFeeds:[filteredArray count]+1 withLimit:20];
NSLog(@"end of the table");
}
}
向上滚动到不调用 API 后的内容高度
将响应数据存储在 NSArray 中,然后添加到 NSMutableArray
NSArray *resObj = [NSArray alloc] init];
NSMutableArray *aMutArray = [NSMutableArray alloc] init];
[aMutArray addObject: resObj];
检查表视图的最后一个索引 在cellForItemAtIndexPath中编写此代码
if(lastIndex)
{
// show load more button
}
取两个部分:第一部分中的行包含要显示的项目,第二部分中包含一行,其单元格具有UIActivityIndicator
。
然后实现给定的委托方法:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if indexPath.section == 1 {
let footercell = cell as! ProductFooterCollectionViewCell
footercell.loader.startAnimating()
fetchSearchProducts()
}
}
表视图 显示单元格时委托 此方法调用:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger totalRow = [tableView numberOfRowsInSection:indexPath.section];//first get total rows in that section by current indexPath.
if(indexPath.row == totalRow-1)
{
// Code...
}
}
希望你的问题能解决。
您需要首先找到表格视图单元格的最后一行。
下面是查找最后一行的代码:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger totalNumberOfRows = [tableView numberOfRowsInSection:indexPath.section];
// isLoadMore flage is TRUE when there is more data available.
if(indexPath.row == totalNumberOfRows-1 && isLoadMore)
{
// Code...
// Here you need to check if there are some load more data is there from pervious web-service or not.
// If there is load more then call web-service and another set of data.
}
}
您可以使用AFNetworking来调用您的Web服务。
希望你能得到逻辑:)