我有一个UITableViewController类,我用viewDidLoad方法下载了代码。 这段代码可以吗?我不确定要下载和显示内容。因为显示新闻需要很长时间,并且表格滚动滞后。对不起,我是新来客观的C.如何快速下载新闻?
@implementation NewsViewController
- (void)viewDidLoad
{
// _sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[super viewDidLoad];
[self getJSON];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return currentCellsCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CellForNews *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
id tempObject=[self.arrayOfNews objectAtIndex:indexPath.row];
cell.publishDate.text=tempObject[@"publish_date"];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:tempObject[@"img_path"]]];
cell.newsImage.image=[UIImage imageWithData:data];
cell.descriptionOfThenews.text=tempObject[@"body"];
cell.titleOfTheNews.text=tempObject[@"publish_title"];
return cell;
}
-(void)getJSON{
NSString *[email protected]"example.com";
NSURL *url=[NSURL URLWithString:path];
NSString *dataJSON=[NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:Nil];
NSData *data=[dataJSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *rootDictionary=[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:nil];
NSDictionary *newsDict=[rootDictionary objectForKey:@"publications"];
id marker=[[NSObject alloc]init];
self.arrayOfNews=(NSMutableArray*)[newsDict objectsForKeys:[newsDict allKeys] notFoundMarker:marker];
currentCellsCount=[newsDict allKeys].count;
}
您在主线程上使用同步网络方法。 - 'stringWithContentsOfURL' – Abizern
这是第二个最重要的问题,也有很多类似的问题。请搜索更难,寻找关键字'UITableView异步“延迟加载”阻止“ – CouchDeveloper
可能重复[在UITableView中的几个图像的延迟加载](http://stackoverflow.com/questions/11583108/lazy-loading-of-several- images-in-uitableview) – CouchDeveloper