移动到下一个 UIViewcontroller,而要在那里显示的数据仍在加载
Move to next UIViewcontroller, while data to be displayed there, is still loading
我正在编写一个 ios 应用程序,它有多个 UIViewcontroller。它们都有 UITableViews,其中填充了数据,这些数据是从不同的 API 中获取的。但我面临的问题是,当我点击一个单元格时,应用程序不会导航到下一页,直到获取该页面的数据。这使应用程序看起来非常慢。我需要一些方法来导航到下一页,在那里我可以放置一些微调动画让用户知道它正在获取数据(至少)。我不希望用户认为该应用程序已崩溃或发生其他情况(它在同一页面中停留 7-10 秒)
提前致谢
我对这个问题的解决方案是,你可以使用调度队列调用API,这样就不会影响其他功能。
您应该在后台调用所有 api,这样它就不会影响主线程,使用队列在后台队列中调用 api,如下所示。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(queue, ^{
//call apis here
});
请遵循这些简单的步骤
ViewController1 = VC1
ViewController1 = VC2
VC1
[self.navigationController pushViewController:VC2 animated:YES];
VC2
@implementation {
BOOL hasData;
}
-(void)viewDidLoad {
hasData = NO;
[self getAndShowData];
}
-(void)getAndShowData {
// Start Showing Spinner
// load your data from server and
// on success
1.) hasData = YES;
2.) Call reload tableview
// Remove spinner
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section {
if(hasData == NO) return 0;
return actual number of rows.
}
}
我正在编写一个 ios 应用程序,它有多个 UIViewcontroller。它们都有 UITableViews,其中填充了数据,这些数据是从不同的 API 中获取的。但我面临的问题是,当我点击一个单元格时,应用程序不会导航到下一页,直到获取该页面的数据。这使应用程序看起来非常慢。我需要一些方法来导航到下一页,在那里我可以放置一些微调动画让用户知道它正在获取数据(至少)。我不希望用户认为该应用程序已崩溃或发生其他情况(它在同一页面中停留 7-10 秒) 提前致谢
我对这个问题的解决方案是,你可以使用调度队列调用API,这样就不会影响其他功能。
您应该在后台调用所有 api,这样它就不会影响主线程,使用队列在后台队列中调用 api,如下所示。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(queue, ^{
//call apis here
});
请遵循这些简单的步骤
ViewController1 = VC1
ViewController1 = VC2
VC1
[self.navigationController pushViewController:VC2 animated:YES];
VC2
@implementation {
BOOL hasData;
}
-(void)viewDidLoad {
hasData = NO;
[self getAndShowData];
}
-(void)getAndShowData {
// Start Showing Spinner
// load your data from server and
// on success
1.) hasData = YES;
2.) Call reload tableview
// Remove spinner
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section {
if(hasData == NO) return 0;
return actual number of rows.
}
}