AFN在子线程加载数据,当需要先加载数据,后显示视图时,将显示视图写在success方法中

//存放所有的地区对象
@property(nonatomic,strong)NSMutableArray *allarray;
 1 -(void)loadRegionData
 2 {
 3     AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
 4     NSString*url=[NSString stringWithFormat:@"%@",CLPLACELIST];
 5     
 6     [manager POST:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
 7         
 8         if (responseObject!=nil) {
 9             
10             NSDictionary *dictionary=responseObject[@"obj"];
11             NSArray *array=dictionary[@"city"];
12             
13             self.allarray=[NSMutableArray array];
14             
15             for (NSDictionary *dict in array) {
16                 RegionModel *regionModel=[[RegionModel alloc] init];
17                 [regionModel setValuesForKeysWithDictionary:dict];
18                 [_allarray addObject:regionModel];
19                 
20             }
21 
22             //九宫格布局
23               [self layoutButtons];
24             
25             
26         }
27  
28 } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
29     NSLog(@"******region*****%@",error);
30     
31 }];
32     
33 
34 }
 1 -(void)layoutButtons
 2 {
 3     
 4     //完成布局设计
 5     
 6     //三列
 7     int totalloc=3;
 8     // 宽度
 9     CGFloat appvieww=85;
10     //高度
11     CGFloat appviewh=30;
12     
13     CGFloat margin=(WIDTHW-totalloc*appvieww)/(totalloc+1);
14     CGFloat marginY=10;
15 
16     for (int i=0; i<self.allarray.count; i++) {
17         int row=i/totalloc;//行号
18         //1/3=0,2/3=0,3/3=1;
19         int loc=i%totalloc;//列号
20         
21         CGFloat appviewx=margin+(margin+appvieww)*loc;
22         CGFloat appviewy=marginY+(marginY+appviewh)*row;
23         
24         //创建地区按钮
25         UIButton *regionBtn=[[UIButton alloc] initWithFrame:CGRectMake(appviewx, appviewy+64, appvieww, appviewh)];
26         [self.view addSubview:regionBtn];
27         //regionBtn.backgroundColor=[UIColor greenColor];
28         NSLog(@"#####buttons###allarray###%@",self.allarray[i]);
29         regionBtn.titleLabel.font=[UIFont systemFontOfSize:12];
30         RegionModel *regionModel=[[RegionModel alloc] init];
31         regionModel=_allarray[i];
32         [regionBtn setTitle:regionModel.name forState:UIControlStateNormal];
33         [regionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
34         [regionBtn addTarget:self action:@selector(didClickRegion) forControlEvents:UIControlEventTouchUpInside];
35         
36     }
37     
38 }
一个人,一片天,一条路,一瞬间!
原文地址:https://www.cnblogs.com/zcl410/p/5038667.html