如果出现 tableview 单元格数据,如何隐藏加载程序?
How to hide loader if tableview cell data appeared?
我将 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];
}
}
已编辑:-
这是我调用的函数 API 并且 setUpData 将在 ViewDidLoad 中调用。
-(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];
}];
}
您可以通过实施 completionHandler
来实现它。
例如:
func fetchApi(completion: (Bool)->()) {
// Getting the data request......
completion(true)
}
像这样调用你的函数:-
fetchApi(completed: Bool) {
if completed {
// Hide your spinner
}
}
我相信您可以在 ObjC 中做同样的事情。希望这会有所帮助。
添加 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];
}];
}
我将 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];
}
}
已编辑:-
这是我调用的函数 API 并且 setUpData 将在 ViewDidLoad 中调用。
-(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];
}];
}
您可以通过实施 completionHandler
来实现它。
例如:
func fetchApi(completion: (Bool)->()) {
// Getting the data request......
completion(true)
}
像这样调用你的函数:-
fetchApi(completed: Bool) {
if completed {
// Hide your spinner
}
}
我相信您可以在 ObjC 中做同样的事情。希望这会有所帮助。
添加 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];
}];
}