2013-06-02 85 views
0

我有一個視圖控制器與表視圖與圖像init。 我加載圖像與AFNetworking,有時(主要是當我的互聯網連接速度很慢),當我點擊在視圖控制器的backbuttonitem和進入RootViewController的應用程序崩潰,我得到以下信息:從桌面視圖控制器返回應用程序崩潰

-[__NSCFDictionary numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x1f567e30 

爲什麼? 使用ARC的Im。 這裏是我的代碼加載圖像

if(item.thumb) 
    { 
     [cell.listItemImage setImage: item.thumb]; 

    } 
    else 
    { 

     UIActivityIndicatorView *loadingSymbol = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray]; 
     loadingSymbol.frame = CGRectMake(0, 0, cell.listItemImage.frame.size.width, cell.listItemImage.frame.size.height); 
     [cell.listItemImage addSubview: loadingSymbol]; 
     [loadingSymbol startAnimating]; 


     [cell.listItemImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: item.thumbUrl]] 
            placeholderImage:[UIImage imageNamed:@"transparent.png"] 
              success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image) 
     { 

       item.thumb = image; 
      [cell setNeedsLayout]; 
       [loadingSymbol removeFromSuperview]; 
       [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

     } 
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) 
     { 
      NSLog(@"something went wrong wiv the images"); 
      NSLog(@"%@", error); 
      //[loadingSymbol removeFromSuperview]; 

     } 
     ]; 
    } 
} 

會很高興,如果有人可以幫助我在這裏!

編輯:

解決了我的第一個問題。但是現在當我在桌面視圖中滾動時,應用程序崩潰。我認爲它與這條線有關:

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

任何想法爲什麼?

+0

根據你管理一個NSDictionaryObject分配給某些屬性,其中框架需要一個表委託的錯誤消息。 –

+0

所以這發生在表視圖控制器已經釋放,但仍有數據在後臺加載。實際上,你保持對不應該存在的單元對象的引用。這些單元格可能仍然存在,因爲ARC依賴於這是強還是弱參考。 –

+0

我不確定。那麼'[cell setNeedsLayout]'是否會導致單元格和表格被重新繪製,而表格或其控制器不再存在?但是,您應該找到一些更智能的解決方案。返回時取消所有打開的請求,或者當相關視圖不可見/不再存在時不處理數據。 –

回答

0

在視圖控制器的dealloc方法中添加以下行。這在我的項目中工作。

[[UIImageView af_sharedImageRequestOperationQueue] cancelAllOperations]; 

你的dealloc看起來像這樣

-(void)dealloc 
{ 
    [[UIImageView af_sharedImageRequestOperationQueue] cancelAllOperations]; 

    //your other clean ups 

    [super dealloc]; 
} 

編輯

在視圖控制器的頭文件,添加一個IBOutlet的UITableView的

IBOutlet UITableView *myTableView;

連接您的UITableView到那個出口(myTableView)。

將IBOutlet中,以零在你的dealloc

-(void)dealloc 
{ 
    myTableView = nil; 
    //your other clean ups 
    [super dealloc]; 
} 

您的成功塊應該是這樣的

success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image) 
     { 
      if(myTableView) 
      { 
       item.thumb = image; 
       [cell setNeedsLayout]; 
       [loadingSymbol removeFromSuperview]; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
      } 

     } 

編輯2(滾動)

在你成功的塊,而不是:

[tableView reloadRowsAtIndexPaths:[NSArray arayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

使用:

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone]; 
+0

謝謝,但我得到以下錯誤,當我做你說什麼:沒有已知的選擇器af_sharedImageRequestOperationQueue類方法? – Jojo

+0

是否在你的視圖控制器文件中放了'#import「UIImageView + AFNetworking.h」'? (包括** .h **文件) –

+0

是的,我已經導入了。 – Jojo

0

問題:網絡I/O在視圖控制器釋放後有一段時間完成。作爲網絡I/O的結果,您可以重新加載一些單元。重新加載單元格調用視圖控制器(解除分配)以獲取數據源信息和崩潰。

解決方案:您必須在dealloc中清零表視圖委託和數據源。

- (void)dealloc { 
    // Cleanup table view 
    self.tableView.delegate = nil; 
    self.tableView.dataSource = nil; 

    // Stop network operations since their are not needed anymore. 
    for (UITableViewCell *cell in [self.tableView visibleCells]) { 
     [cell. listItemImage cancelImageRequestOperation]; 
    } 
} 

在你的cellForRowAtIndexPath取消圖像下載在重新使用前:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"MyCellId"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // Cancel network request 
    [cell. listItemImage cancelImageRequestOperation]; 
    ... 
} 
+0

我也是這樣做的,但是現在我上下滾動時,我的應用崩潰了。互聯網連接速度慢時會發生。任何想法爲什麼? – Jojo

+0

我正在使用UIImageView + AFNetworking,我想我正確地使用它..我也得到這個消息,不知道它是什麼意思:purgeIdleCellConnections:找到一個清除conn = 0x1d5bb9a0 – Jojo

+0

https:// developer .apple.com/library/ios /#qa/qa1774/_index.html#// apple_ref/doc/uid/DTS40012992 –

相關問題