ios - 将数组加载到 tableView 中?

标签 ios objective-c uitableview

我一直这样做,但由于某种原因它今天对我不起作用。

单元格为空,这是我的代码:

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

    return [self.messageArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell..
    NSString *messageAtIndexPath = [messageArray objectAtIndex :[indexPath row]];
    [[cell textLabel] setText:messageAtIndexPath];

    return cell;
}

我确保所有内容(委托(delegate)和源)都正确连接。

如果我硬编码一些东西,例如:

return 5;

[[cell textLabel] setText:@"this is a cell"];

然后它就可以工作了,所以我不确定发生了什么。我的数组也已填充,我可以在日志控制台中看到它。

编辑:

dispatch_async(queue, ^{
    NSString *postString = @"controller=Push&action=GetPushAlerts&accountId=123";
    NSData *JsonData = [ApiCaller post :postString];
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:JsonData options:NSJSONReadingMutableContainers error:nil];
    //parsing JSON

    dispatch_async(dispatch_get_main_queue(), ^{

        BOOL success = [result[@"success"] boolValue];
        if(success){
            // Account exists, parse the data
            NSDictionary *data = [result objectForKey:@"data"];
            messageArray = [data objectForKey:@"message"];

           // NSLog(@"count %@", message);

        }

    });
});

最佳答案

总结一下:如果您异步加载数据,则必须重新加载 TableView (或任何显示数据并在数据到达之前绘制的内容),因为当 View 加载时,它会根据它在那一刻拥有的数据。

为此,请将以下命令添加到数据到达的位置(通常添加到成功 block 中,除非您还想在失败 block 上显示一些数据):

[self.tableView reloadData]; 

如果另一方面,调用是同步的(很少使用),它会挂起执行调用的应用程序(通常在viewDidLoad中),因此只有在数据到达后才绘制显示,从而有数据要显示。

关于ios - 将数组加载到 tableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21612254/

相关文章:

objective-c - 哪种方式存储数据(图像)? NSData、字符串或可转换

ios - uitableview 中的单元格分隔线未覆盖整个宽度

iphone - 什么是 Apple App Store 中应用程序的 "current version"?

ios - popViewController 抛出 "deallocated while key value observers were still registered"错误

iphone - iOS - 检测游戏中心是否被禁用

objective-c - 从字符串中删除空格

ios - 如何从 json 数组访问值

ios - 运行一个循环直到 UIPanGestureRecognizer 结束

ios - 如何在我的单元格中更改附件类型 -> 披露指示器颜色?

objective-c - 带有 UILabel subview 缓存问题的 UITableViewCell