我将CircularSpinner加载器应用到我的tableview列表中。如果已加载单元格,如何隐藏加载程序?现在,每次访问都会显示加载程序。
示例代码如下: -
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[CircularSpinner show:@"Loading" animated: TRUE type:CircularSpinnerTypeDeterminate showDismissButton:[NSNumber numberWithBool:TRUE] delegate:self];
[CircularSpinner setValue:0.4 animated: TRUE];
}
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
[CircularSpinner setValue:1.0 animated: TRUE];
}
}
EDITED: -
这是我调用API的函数,将在ViewDidLoad中调用setUpData。
-(void)setUpData{
[self.manager GET:@"http://api.XXX.com/api/announcement" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
_serverDataArr = responseObject;
self.dataArr=[NSMutableArray array];
for (NSDictionary *subDic in self.serverDataArr) {
Announcement_Model *model=[[Announcement_Model alloc]initWithDic:subDic];
[self.dataArr addObject:model];
}
_rowArr=[Events_DataHelper getFriendListDataBy:self.dataArr];
_sectionArr=[Events_DataHelper getFriendListSectionBy:[_rowArr mutableCopy]];
[self.tableView reloadData];
} failure:^(NSURLSessionTask *operation, NSError *error) {
// [CircularSpinner setValue:1.0 animated: TRUE];
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Please try again"
message:[error localizedDescription]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleCancel
handler:nil];
[alertVC addAction:okAction];
[self presentViewController:alertVC animated:YES completion:nil];
}];
}
答案 0 :(得分:1)
您可以通过实施completionHandler
来实现这一目标。
例如:
func fetchApi(completion: (Bool)->()) {
// Getting the data request......
completion(true)
}
按照以下方式调用您的函数: -
fetchApi(completed: Bool) {
if completed {
// Hide your spinner
}
}
我相信你可以在ObjC做同样的事情。希望这会有所帮助。
答案 1 :(得分:0)
添加CircularSpinner.hide()
,因为我在下面的代码中进行了标记。
希望self.manager
调用API函数,并在此处给出闭包中的响应。
-(void)setUpData{
[self.manager GET:@"http://api.XXX.com/api/announcement" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
[CircularSpinner hide]; // <- (1)
_serverDataArr = responseObject;
self.dataArr=[NSMutableArray array];
for (NSDictionary *subDic in self.serverDataArr) {
Announcement_Model *model=[[Announcement_Model alloc]initWithDic:subDic];
[self.dataArr addObject:model];
}
_rowArr=[Events_DataHelper getFriendListDataBy:self.dataArr];
_sectionArr=[Events_DataHelper getFriendListSectionBy:[_rowArr mutableCopy]];
[self.tableView reloadData];
} failure:^(NSURLSessionTask *operation, NSError *error) {
// [CircularSpinner setValue:1.0 animated: TRUE];
[CircularSpinner hide]; // <- (2)
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Please try again"
message:[error localizedDescription]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleCancel
handler:nil];
[alertVC addAction:okAction];
[self presentViewController:alertVC animated:YES completion:nil];
}];
}