代码之家  ›  专栏  ›  技术社区  ›  LUI

应用程序崩溃,原因是集合表达式类型UIView*可能无法响应countByEnumeringWithState:object:count“”

  •  2
  • LUI  · 技术社区  · 11 年前

    我有一个 UIImageView 并希望在单击该行时以编程方式删除它。

       -(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" 应用程序崩溃也会收到同样的消息。我错过了什么?我只粘贴了代码的相关部分。

    3 回复  |  直到 11 年前
        1
  •  17
  •   Anil Varghese    11 年前

    代码应类似

    for (UIView *subview in  tableView.subviews)
    
        2
  •  0
  •   Akhilrajtr    11 年前

    将循环更改为

      for (UIView *subview in  [tableView subviews])
      {
           if([subview isKindOfClass:[UIImageView class]])
           {
               [subview removeFromSuperview];
           }
      }
    
        3
  •  0
  •   Mani    11 年前

    尝试以下代码。它可能会帮助你。。

    -(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.
        }
    

    }