2014-03-13 10 views
1

我有一個UIImageView一個的tableView內,要在單擊行時以編程方式刪除它應用程序崩潰說集合表達式類型的UIView * countByEnumeratingWithState」可能不響應「:對象:伯爵「」

-(void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

      for (UIView *subview in tableView) 
      { 
       if([subview isKindOfClass:[UIImageView class]]) 
       { 
       [subview removeFromSuperview]; 
       } 
      } 
} 

當我點擊應用程序崩潰的錶行。Warning : Collection expression type UIView* may not respond to countByEnumeratingWithState:object:count" 同一封郵件遞交該應用程序崩潰。我在這裏錯過了什麼?我只粘貼代碼的相關部分。

回答

13

代碼應該像

for (UIView *subview in tableView.subviews) 
+0

這實際上回答我的問題,以及!但我不確定爲什麼。你有什麼見解嗎? – narner

+1

@narner在他試圖刪除一個tableView的子視圖的問題,但他錯誤地寫了'for(UIView * subview in tableView)'迭代。這裏'tableView'是一個單一的視圖。我們應該得到它的子視圖'tableView.subviews'。 –

+0

啊,謝謝你! – narner

0

改變循環到下面的代碼

for (UIView *subview in [tableView subviews]) 
    { 
     if([subview isKindOfClass:[UIImageView class]]) 
     { 
      [subview removeFromSuperview]; 
     } 
    } 
0

嘗試。它可以幫助你..

-(void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     NSArray *cells = [tableView visibleCells]; 
     UITableViewCell *currentCell = nil; 
     for (UITableViewCell *cell in cells) 
     { 
      NSIndexPath *currentIndexPath = [tableView indexPathForCell:cell]; 
      if(currentIndexPath.row == indexPath.row && currentIndexPath.section == indexPath.section) 
      { 
       currentCell = cell; 
       break; 
      } 
     } 
    for(UIView *subView in [[currentCell contentView] subviews]) 
    { 
     // do your stuff. 
    } 

}

相關問題