应用崩溃指出集合表达式类型 UIView*' 可能无法响应"countByEnumeratingWithState:object:count""



我在表图内有一个 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"应用程序崩溃传达了相同的消息。我在这里错过了什么?我只粘贴了代码的相关部分。

代码应该像

for (UIView *subview in  tableView.subviews)

将循环更改为

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

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

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

}

最新更新