2015-01-06 53 views
1

我試圖從服務器異步加載圖像到單元格。但滾動時圖像不會更改,只有在滾動停止後纔會更改。只有在滾動停止後,「加載」消息纔會出現在控制檯中。我希望圖像在滾動時出現在單元格中。滾動時更新UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    CustomCell *cell = (CustomCell *)[_tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
    ZTVRequest *request = [[ZTVRequest alloc] init]; 
    [request getSmallImg completionHandler:^(UIImage *img, NSError *error) { 
     if (! error) { 
      NSLog(@"loaded") 
      cell.coverImgView.image = img; 
     } 
    }]; 

    return cell; 
} 
+1

您的'cell.coverImgView.image = omg;'似乎在後臺線程上運行,因此不會立即更新。您必須使用像dispatch_async(dispatch_get_main_queue(),^ cell.coverImgView.image = img; })'調度圖像加載到主隊列。請參閱:http://stackoverflow.com/q/4944363/558933 –

+0

沒有幫助。我已經添加了答案。 – user1561346

回答

1

我正在使用NSURLConnection加載圖像。我發現這個答案的解決方案:https://stackoverflow.com/a/1995318/1561346通過丹尼爾Dickison

這就是:

連接代理消息沒有射擊,直到你停止滾動的原因是因爲在滾動過程中,運行循環是在UITrackingRunLoopMode 。默認情況下,NSURLConnection僅在NSDefaultRunLoopMode中安排,因此您在滾動時不會收到任何消息。

下面是如何安排的「普通」模式的連接,其中包括UITrackingRunLoopMode

NSURLRequest *request = ... 
NSURLConnection *connection = [[NSURLConnection alloc] 
           initWithRequest:request 
           delegate:self 
           startImmediately:NO]; 
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] 
      forMode:NSRunLoopCommonModes]; 
[connection start]; 

注意,您必須在初始化,這似乎有悖於蘋果的文檔表明您指定startImmediately:NO即使在啓動後也可以改變運行循環模式。

相關問題