我一直这样做,但由于某种原因它今天对我不起作用。
单元格为空,这是我的代码:
-(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/