我的 UITableView
上有“滚动加载更多”行为。当您滚动到底部时,它开始加载更多内容。我想要实现的是,当服务调用正在进行时,在 UITableView 的底部显示一个 UIActivityIndicatorView
,并在添加新项目时隐藏它。
有什么好的方法可以解决这个问题吗?我正在使用 Swift,但也欢迎 Objective-C 解决方案。
最佳答案
我在 Objective-C 中实现了相同的功能
在cellForRowAtIndexPath
if (indexPath.row == [self.yourArrayData count] - 1)
{
// Call the API
[self loadMoreData:@"all" andPages:[NSString stringWithFormat:@"%ld", (long)pages] AndIsLoadMore:YES];
}
添加加载程序或设计
-(void)addLoaderViewAtBottom {
viewLoader = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 50)];
[viewLoader setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:viewLoader];
UIActivityIndicatorView *activityIndicator1 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator1.center = CGPointMake((SCREEN_SIZE.width/2), 30);
activityIndicator1.alpha = 1.0;
activityIndicator1.hidden = NO;
[viewLoader addSubview:activityIndicator1];
[activityIndicator1 startAnimating];
}
-(void)hideLoaderView {
[UIView animateWithDuration:1
animations:^(void) {
[viewLoader setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50)];
}];
}
然后在您的 API 调用方法中使用这两个方法。 API 调用开始时调用 addLoaderViewAtBottom
并在获取响应后调用 hideLoaderView
编码愉快...
关于ios - 如何将 UIActivityIndicatorView 添加到 UITableView 的底部,并在加载时切换它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40546702/