2014-02-21 34 views
1

我有一个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; 


} 
+0

您在主线程上使用同步网络方法。 - 'stringWithContentsOfURL' – Abizern

+0

这是第二个最重要的问题,也有很多类似的问题。请搜索更难,寻找关键字'UITableView异步“延迟加载”阻止“ – CouchDeveloper

+0

可能重复[在UITableView中的几个图像的延迟加载](http://stackoverflow.com/questions/11583108/lazy-loading-of-several- images-in-uitableview) – CouchDeveloper

回答

0

不要三思。对于任何联网操作,请使用AFnetworking并且对于任何图像网页加载使用SDWebImage

3

,因为它需要大量的时间来显示新闻 你应该想到预加载您的数据。

和表格滚动滞后。

这是因为您正在同步执行网络操作,这会阻止负责呈现UI元素的应用程序主线程。所以你应该发送你的请求asynchronously

那么,一旦你了解基础知识。更好地诉诸图书馆,可以做得比这更多,如AFNetworking

我有一个UITableViewController类,我下载了viewDidLoad方法的代码。该代码可以吗?

我不能说这是不好的,neverthless它的作品!但可能不理想,为什么它滞后?

因此,我有没有自由回答你的问题,而是给出一个更好的设计应用逻辑的建议? 如果是这样读的话。

什么是viewDidLoad?
  • viewDidLoad中是你的view controller life cycle,它会告诉你的视图控制器,一部分“嘿伙计!你所有的观点都在这里装和准备。做你的狗屎“时间现在”。是!这是你应该对你的观点采取一些行动的地方。不在你的数据上(除非它直接对视图有所作为)。

这里我们程序员用的Seperation Of Concerns古老的技术开明和它的一个形式,通过iOS的僧侣实行全球各地的Model-View-Controller。所以你应该寻找这样的路径或另一个像MVVM

一旦你有了这些分离,新闻是数据,将完全脱离你的视野,然后你可以考虑更快加载它的技术。就像在初始化应用程序时加载它,或者使用基于UI的基于UI的延迟加载模式来刷新或其他任何东西。