您的位置:首页 > 移动开发 > IOS开发

浅谈ios异步加载

2016-03-18 19:55 344 查看
需求情况:在ViewDidLoad中访问数据,根据数据库返回的数据来实现tableView的datasource

- (void)viewDidLoad {

    [LoadingView showLoadingViewWithMessage:@"数据操作中"];

    NSDictionary *parameters = @{@"type":@"1"};

    [FeedBackOpinionModel appPayFeedBackWithParameters:parameters success:^(NSString *jsonString) {

        [LoadingView hideLoadingView];

        NSDictionary *jsonDict = [jsonString objectFromJSONString];

        }

        NSMutableArray *arrayM = [jsonDict objectForKey:@"data"];

        for (NSDictionary *dict in arrayM) {

            NSString *appPayFeedbackId = dict[@"id"];

            [payFeedbackIdArray addObject:appPayFeedbackId];

            NSString *titleStr = dict[@"title"];

            [titleArray addObject:titleStr];

        }

        [self.tableView reloadData];//调用UITableView的dataSource

    } fail:^{

    }];

}

#pragma mark UITableDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return titleArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableView *cell = [tableView dequeueReusableCellWithIdentifier:CellID];

    if (cell == nil) {

       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

    };

    cell.textLabel.text = titleArray[indexPath.section];

    return cell;

}

注:在ViewDidLoad中访问接口时,或许还在向数据库拿数据的时候;整个View已经加载完毕了,所以没有拿到数据,这时tabelView的dataSource为空的。在访问接口时调用[self.tableView reloadData]这个方法,可以刷新dataSource的三个方法,拿到里面的数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitableview