【发布时间】:2014-09-15 15:01:47
【问题描述】:
我正在构建一篇文章阅读应用程序。我正在使用 UITableView 中的 NSData 解析 JSON 数据。
我遇到了一个问题,即网速慢(2g 或 3g)无法加载数据意味着 UI 是空的。
我想实现 NSUrlConnection
但我是 iOS 开发新手,无法在我的代码中实现 NSUrlConnection。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL myBool = [self isNetworkAvailable];
if (myBool)
{
@try {
// for table cell seperator line color
self.tableView.separatorColor = [UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:1.0];
UIBarButtonItem *backbutton1 = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backbutton1];
_Title1 = [[NSMutableArray alloc] init];
_Author1 = [[NSMutableArray alloc] init];
_Images1 = [[NSMutableArray alloc] init];
_Details1 = [[NSMutableArray alloc] init];
_link1 = [[NSMutableArray alloc] init];
_Date1 = [[NSMutableArray alloc] init];
NSData* data = [NSData dataWithContentsOfURL:ysURL];
NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if(ys_avatars){
for (int j=0;j<ys_avatars.count;j++)
{
if( ys_avatars[j][@"title"]==[NSNull null] ){
[_Title1 addObject: @""];
}
else{
[_Title1 addObject:ys_avatars[j][@"title"]];
}
if( ys_avatars[j][@"author"]==[NSNull null] ){
[_Author1 addObject: @""];
}
[_Author1 addObject: ys_avatars[j][@"author"]];
if( ys_avatars[j][@"featured_img"]==[NSNull null] ){
[_Images1 addObject: @""];
}
else{
[_Images1 addObject: ys_avatars[j][@"featured_img"]];
}
if( ys_avatars[j][@"content"]==[NSNull null] ){
[_Details1 addObject: @""];
}else{
[_Details1 addObject:ys_avatars[j][@"content"]];
}
if( ys_avatars[j][@"permalink"]==[NSNull null] ){
[_link1 addObject: @""];
}
else{
[_link1 addObject:ys_avatars[j][@"permalink"]];
}
if( ys_avatars[j][@"date"]==[NSNull null] ){
[_Date1 addObject: @""];
}
else{
NSString *newStr=[ys_avatars[j][@"date"] substringToIndex:[ys_avatars[j][@"date"] length]-3];
[_Date1 addObject:newStr];
}
}
}
else
{
NSLog(@"asd");
}
}
@catch (NSException *exception) {
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Cellidentifier1 = @"ysTableViewCell";
ysTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
long row = [indexPath row];
cell1.TitleLabel1.text = _Title1[row];
cell1.AuthorLabel1.text = _Author1[row];
NSString *StoryUrl = [_Images1[indexPath.row] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
if(StoryUrl) {
NSArray *subStringsUrl = [yourStoryUrl componentsSeparatedByString:@"/"];
NSString *stripedName = [subStringsUrl lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* filePath =[documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@",stripedName]];
if(filePath) {
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
if(image) {
ysTableViewCell *updateCell =(id)[tableView cellForRowAtIndexPath:indexPath];
if(updateCell)
updateCell.ThumbImage1.image=image;
cell1.ThumbImage1.image=image;
} else {
dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(taskQ, ^{
NSURL *Imageurl = [NSURL URLWithString:yourStoryUrl];
NSData *data = [NSData dataWithContentsOfURL:Imageurl];
UIImage *images1 = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImagePNGRepresentation(images1);
if (![imageData writeToFile:filePath atomically:NO])
{
NSLog((@"Failed to cache image data to disk"));
}
else
{
NSLog(@"the cachedImagedPath is %@",filePath);
}
dispatch_sync(dispatch_get_main_queue(), ^{
ysTableViewCell *updateCell =(id)[tableView cellForRowAtIndexPath:indexPath];
if(updateCell)
updateCell.ThumbImage1.image=images1;
cell1.ThumbImage1.image=images1;
});
});
}
return cell1;
}
感谢您的帮助。 提前致谢。
【问题讨论】:
标签: ios objective-c json uitableview nsurlconnection