我正在尝试使用情节提要和 JSON 创建一个松散版本的 LazyTabelImages。在我的主 TableViewController 上的 ViewDidLoad 中,我启动了一个 NSURLConnection 来获取 JSON 数据,但我的单元格直到连接完成后才会加载。我想要与 LazyTableImages 相同的行为,其中单元格加载为空白,然后填充信息(重新加载表数据)。如果我不使用情节提要,我可以复制它,因为 LazyTables 不使用情节提要,但这不是一个选项。
我查看了 LazyTableImages 试图找到解决方案,但故事板有很大的不同(无论如何对我来说)。
有没有一种简单的方法可以让单元格加载为空白?例如,如果设备没有互联网,我仍然希望我的 TableView 显示,我会在单元格中放置一条自定义消息。
代码:
我在 viewDidLoad 中初始化连接的部分......
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
self.dataConnection = [[NSURLConnection alloc] initWithRequest:urlrequest delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
connectionDidFinnishLoading...
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//ListData below is an array that my data received (JSON) is loaded into. It is then passed to getTableData.
self.dataConnection = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self performSelectorOnMainThread:@selector(getTableData:) withObject:ListData waitUntilDone:YES];
});
}
获取表数据...
-(void)getTableData:(NSData *)jsonData
{
NSError *error = nil;
arrayEntries = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
for (int x = 0; x < arrayEntries.count; x++)
{
NSMutableDictionary *dic = [arrayEntries objectAtIndex:x];
//ARecord is a class just like in LazyTableImages that creates objects to keep the icons/data together. The ARecords are loaded into the TableView
ARecord *arecord = [[ARecord alloc] init];
NSString *title = [dic objectForKey:@"title"];
NSString *subt = [dic objectForKey:@"subtitle"];
NSString *url = [dic objectForKey:@"image_URL"];
arecord.Icon = nil;
arecord.URL = url;
arecord.Name = title;
arecord.title = subt;
//this is where I load an array full of the arecord objects.
[array addObject:arecord];
}
[self.tableView reloadData];
}