ios - 异步加载数据但按顺序填充 View
<p><p>我有一个用例,我使用多个数据源异步加载一些数据。因此,每个数据源都有一个完整的方法,我的 ViewController 实现了一个定义了该方法的协议(protocol)。例如,我的一个数据源是 fetchApples,它向我的 Controller 返回一个 Fruit 对象数组,另一个数据源是 fetchOranges 等。在我的 viewController 中,我希望先出现苹果,然后是橙子,然后是葡萄(我填充了一个 uiview带有用于渲染水果的自定义单元格)。如果没有苹果,橙子应该首先出现等。当我的数据源被异步返回时,我如何映射这个顺序。 IE。当橙子回来时,我不知道我是否会有苹果,因此我还不能用它们填充 uiview?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使用 <code>dispatch_group</code>。每个服务器调用都会填充自己的项目数组,您可以将其添加到一个主数组(数据源)或单独使用它们。</p>
<p>所有服务器调用完成后,您会收到通知,然后您可以使用所需的任何数据源以您想要的任何顺序重新加载您的表..</p>
<p>示例(使用 <code>dispatch_group</code> 和 <code>UITableView</code>):</p>
<pre><code>//
//ViewController.m
//StackOverflowExample
//
//Created by Brandon Anthony on 2016-07-16.
//Copyright © 2016 XIO. All rights reserved.
//
#import "ViewController.h"
typedef NS_ENUM(NSInteger, Fruit) {
Apples,
Oranges,
Grapes
};
#define kImageCellIdentifier @"kImageCellIdentifier"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIButton *testButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *apples;
@property (nonatomic, strong) NSMutableArray *oranges;
@property (nonatomic, strong) NSMutableArray *grapes;
@end
@implementation ViewController
- (void)viewDidLoad {
;
_apples = [ init];
_oranges = [ init];
_grapes = [ init];
;
;
;
;
;
}
- (void)didReceiveMemoryWarning {
;
}
- (void)initControls {
_testButton = [ init];
_tableView = [ initWithFrame:CGRectZero style:UITableViewStyleGrouped];
}
- (void)setTheme {
;
];
forState:UIControlStateNormal];
[ setCornerRadius:5.0f];
;
;
;
}
- (void)registerClasses {
forCellReuseIdentifier:kImageCellIdentifier];
}
- (void)doLayout {
;
;
NSDictionary *views = @{@"testButton":_testButton, @"tableView":_tableView};
NSMutableArray *constraints = [ init];
-%d-|", 150, 15]];
-%d-|", 0, 0]];
-%d--%d-|", 25, 44, 10, 0]];
for (NSString *constraint in constraints) {
];
}
for (UIView *view in self.view.subviews) {
;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger sectionCount = _apples.count ? 1 : 0;
sectionCount += _oranges.count ? 1 : 0;
sectionCount += _grapes.count ? 1 : 0;
return sectionCount ? sectionCount : 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return _apples.count ?: _oranges.count ?: _grapes.count ?: 0;
}
if (section == 1) {
return _oranges.count ?: _grapes.count ?: 0;
}
return _grapes.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return _apples.count ? @"Apples" : (_oranges.count ? @"Oranges" : (_grapes.count ? @"Grapes" : @"No Fruits"));
}
if (section == 1) {
if (_apples.count) {
return _oranges.count ? @"Oranges" : (_grapes.count ? @"Grapes" : @"No Fruits");
}
}
return @"Grapes";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
Fruit fruit = _apples.count ? Apples : (_oranges.count ? Oranges : (_grapes.count ? Grapes : 0));
return ;
}
if (indexPath.section == 1) {
if (_apples.count) {
Fruit fruit = _oranges.count ? Oranges : (_grapes.count ? Grapes : 0);
return ;
}
}
return ;
}
- (UITableViewCell *)cellForFruit:(Fruit)kind tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = ;
switch (kind) {
case Apples: {
NSString *kind = ;
[ setImage:nil]; //Some Image..
[ setText:kind];
}
break;
case Oranges: {
NSString *kind = ;
[ setImage:nil]; //Some Image..
[ setText:kind];
}
break;
case Grapes: {
NSString *kind = ;
[ setImage:nil]; //Some Image..
[ setText:kind];
}
break;
default:
break;
}
return cell;
}
- (void)loadData {
;
;
;
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self getApples:^(NSArray *apples) {
if (apples.count) {
@synchronized (self.apples) {
;
}
}
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[self getOranges:^(NSArray *oranges) {
if (oranges.count) {
@synchronized (self.oranges) {
;
}
}
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[self getGrapes:^(NSArray *grapes) {
if (grapes.count) {
@synchronized (self.grapes) {
;
}
}
dispatch_group_leave(group);
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
;
});
}
//Simulating server calls..
- (void)getApples:(void(^)(NSArray *apples))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *apples = @[@"Red Apple", @"Sweet Apple", @"Sour Apple"];
bool error = arc4random_uniform(100) <= 50;
if (error) {
completion(nil);
}
else {
completion(apples);
}
});
}
- (void)getOranges:(void(^)(NSArray *oranges))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *apples = @[@"Tiny Orange", @"Bruised Orange", @"Red Orange"];
bool error = arc4random_uniform(100) <= 50;
if (error) {
completion(nil);
}
else {
completion(apples);
}
});
}
- (void)getGrapes:(void(^)(NSArray *grapes))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *apples = @[@"Baby Grapes", @"Green Grapes", @"I ran out of ideas for names.. Grapes"];
bool error = arc4random_uniform(100) <= 50;
if (error) {
completion(nil);
}
else {
completion(apples);
}
});
}
@end
</code></pre>
<p> <a href="https://i.imgur.com/S1DiqGW.png" rel="noreferrer noopener nofollow">http://i.imgur.com/S1DiqGW.png</a> </p>
<p> <a href="/image/ruBoA.png" rel="noreferrer noopener nofollow"><img src="/image/ruBoA.png" alt="enter image description here"/></a> </p></p>
<p style="font-size: 20px;">关于ios - 异步加载数据但按顺序填充 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38416479/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38416479/
</a>
</p>
页:
[1]