ios - 从 View Did Load 中检索到的数据创建 TableView Controller

标签 ios objective-c json uitableview viewdidload

您好,我是创建 TableView Controller 的新手。我在尝试将 ViewDidLoad 部分中通过 Json 检索到的数据加载到 TableView Controller 时遇到困难。 我注意到的第一件事是,我最终放置 json 数据 (dataArray) 的数组在:numberOfRowsInSection 中不可用,因此我在 viewDidLoad 之外声明了它,但现在看到,当我调试时,数组在到达 numberOfRowsInSection 时为空。我事先测试了检索代码,所以我确信它能正常工作。 我有一些问题: 1. 为什么dataArray在其他方法中是空的? 2. 在 ViewDidLoad 中加载用于在 TableView 中显示的数据是否可以,或者我真的应该将该数据加载到其他更可见且可供 TableView 方法使用的地方吗?

代码如下:

NSArray *dataArray = nil;


- (void)viewDidLoad {
    [super viewDidLoad];

    dataArray = [[NSArray alloc] init];

    //  Set URL variable with Token
    NSString *token = @"99fdf505547d607e65b8b8ff16113337";
    NSString *teacherUrl = [NSString stringWithFormat: @"https://covle.com/apps/api/nextdoorteacher/teachers?t=%@",token];

    //  Set up the session configuration
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];

    [[session dataTaskWithURL:[NSURL URLWithString:teacherUrl]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {
                NSLog(@"response ==> %@", response);

                if (!error) {
                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                    if (httpResponse.statusCode == 200){

                        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];

                        NSLog(@" heres the serialised json ===> %@", jsonData);
                        // move serialised JSON into Array (as the data contained is in array)

dataArray=(NSArray*)[json数据复制];

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

    // Return the number of sections.
    return 1;
}

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

    // Return the number of rows in the section.
    return dataArray.count;
    NSLog(@"Array count is set to ===> %@", dataArray.count);

如果您能帮助理解和解决这个问题,我们将不胜感激。 谢谢

最佳答案

为此 NSArray *dataArray=(NSArray*)[jsonData copy]; 你正在创建一个名为 dataArray 的新局部变量,它与你在 viewDidLoad 中分配的全局 dataArray 不同,所以只写这个 dataArray = (NSArray*)[jsonData copy]; 您将在 numberOfRowsInSection

中获得预期的数组计数

编辑: 在 dataArray = (NSArray*)[jsonData copy] 之后调用 [self.tableview reloadData] 调用 tableview 委托(delegate)方法再次触发!

关于ios - 从 View Did Load 中检索到的数据创建 TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32293411/

相关文章:

ios - 我如何在 iphone 的 objective c 中为短信和铃声设置振动?

javascript - Node.js 解析 JSON

javascript - 如何设置加载 JSON 层的最小缩放?

iphone - 推送的 ViewController 是 "greyed out"不能与它保持一致

ios - 一段时间后动画在 iOS 应用程序中消失

iOS - 如何替换已经存在的本地 sqliteDB

ios - 根据确切的 URL 隐藏按钮

objective-c - 大中央调度。如何运行一个异步组,等待,然后运行另一个,再次等待,然后完成?

iphone - 如何在 Objective C 中使用静态变量(BOOL)

ios - 如何在 Swift 中将 Json 模型数据存储和提取到 Coredata