我想使用故事板和JSON创建一个松散版本的LazyTabelImages。在我的主TableViewController的ViewDidLoad中,我启动一个NSURLConnection来获取JSON数据,但是我的单元格在连接完成之后才加载。我希望LazyTableImages具有相同的行为,其中单元格加载为空白,但是然后填充信息(重新加载表格数据)。如果我不使用storyboard,我可以复制它,因为LazyTables不使用storyboard,但这不是一个选项。iPhone - 使UITableViewCells出现在NSURLConnection完成之前的视图加载
我已经浏览了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];
});
}
getTableData ...
-(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];
}
您是否在主线程上调用网络?如果那样的话,联网块UI线程。 – jessex
我已经尝试使用GCD将它从主线程移出,但之后我只是得到一个白色的屏幕,因为表永远不会加载。 NURLConnection中有一个GCD调用,用于处理不同线程上的JSON解析。 – Siriss
请注意,您的NSURLConnection(应该)已经是异步的。将它移动到后台线程可能是一个糟糕的主意。正如Quinn所说,网络关注“线程是邪恶的”。 – jemmons