![](/img/trans.png)
[英]What is a good way to display a spinner at the bottom of a tableview when a user scrolls, but the tableview cells load slowly?
[英]Load more cells when user scrolls to bottom of tableview frame
当用户像这样滚动到底部时,我正在向tableView
添加新数据。
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
float height = self.itemListTableView.frame.size.height;
float contentYoffset = aScrollView.contentOffset.y;
float distanceFromBottom = aScrollView.contentSize.height - contentYoffset;
if (distanceFromBottom < height) {
NSLog(@" you reached end of the table");
isTableScrolledToBottom = YES;
}else{
isTableScrolledToBottom = NO;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"CellIdentifier";
ListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell = [[ListViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Assign values to labels here..
if (isTableScrolledToBottom) {
if (indexPath.row == [apiMutableArray count]-1)
{
// Call the API
NSLog(@"Call the api");
int pgn = [self.currentPageNumber intValue];
pgn = pgn+1;
self.currentPageNumber = [NSNumber numberWithInt:pgn];
NSLog(@"incremented page number : %@",self.currentPageNumber);
NSMutableDictionary *mutableDict = [self.poPostDict mutableCopy];
[mutableDict setObject:self.currentPageNumber forKey:@"pageno"];
self.poPostDict = [mutableDict mutableCopy];
[self addLoaderViewAtBottom];
[self loadPOData:self.poPostDict];
}
}
return cell;
}
现在,如果表视图中的数据足够大,则用户将滚动到底部并按预期从api进行数据获取。但是,当我过滤此表并说我仅获得一行数据时。 在这种情况下,即使我只能看到表格中存在一个单元格,但如果尝试滚动表格,也会遇到api请求。 如何真正知道用户是否已滚动到其frame
之外并滚动到scrollView's
末端,以便仅在这种情况下才能获取数据。
注意:-如果当前仅存在一行并且尝试滚动,则使用当前技术,api调用会连续命中而不会停止。
尝试此解决方案,它始终对我有用。
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let lastElement = dataSource.count - 1
if indexPath.row == lastElement {
// logic here
}
}
如果您仍有疑问,请询问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.