0

我已经尝试解决这个问题很长一段时间了,但我仍然无法弄清楚。我拥有的是故事板中的自定义表格视图单元格。在单元格中,我添加了 6 个视图,每个视图都有一个 imageView 子视图。我设置了视图的标签,以便以后可以访问它们。此表用作我的应用程序中的缩略图视图。问题是,在特定行中,最后一个缩略图容器视图不再将 imageview 作为子视图,从而导致崩溃。

下面是我为表格设置图像的代码。任何帮助将不胜感激。谢谢!

  NSString *CellIdentifier = @"ThumbnailCell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    //loops through each thumbnails
    for (int i=1; i<=kNumberOfThumbnails; i++)
    {

        //get index of current thumbnail- starting value is 0
        int index=((indexPath.row *kNumberOfThumbnails)+i)-1;
        NSLog(@"index %d",index);
        //create an indexpath given the computed index and cell section
        NSIndexPath *currentIndexPath=[NSIndexPath indexPathForRow:index inSection:indexPath.section];
        //get number of sections
        NSArray *sections = [self.fetchedResultsController sections]; 

        NSInteger count=0;
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:indexPath.section];
        //get number of objects for given section
        count = [sectionInfo numberOfObjects]; 

        NSLog(@" i: %d  number of subviews ni thumn container %d",i,thumbContainer.subviews.count);

         //get view container for thumbnails
        UIView *thumbContainer=(UIView *)[cell.contentView viewWithTag:i];


        UIImageView *imageView=[thumbContainer.subviews objectAtIndex:0];// this is where the app crashes.. thumbContainer no longer have a subview (for a specific row only)so it throws out an nsrangeexception

        if (index<count) 
        {

            //get file using the created indexpath
            File *imageFile=[self.fetchedResultsController objectAtIndexPath:currentIndexPath];

            //set image
            imageView.image=[UIImage imageNamed:imageFile.thumbnail];
            thumbContainer.backgroundColor=[UIColor grayColor];

            //set tag for image view for the system to know what file is tapped
            imageView.tag=(currentIndexPath.section*kImageTagMultiplier)+currentIndexPath.row;

            //add tap gesture to thumbnail container view

            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resultTapped:)];
            tap.numberOfTapsRequired=1;
            [thumbContainer addGestureRecognizer:tap];   


        }
        else {
            imageView.image=[UIImage imageNamed:@""];
            thumbContainer.backgroundColor=[UIColor clearColor];
            for (UIGestureRecognizer *gest in thumbContainer.gestureRecognizers) {
                [thumbContainer removeGestureRecognizer:gest];
            }
        }


   return cell;

我在这里尝试做的是我有一个对象数组,每个对象都由一个缩略图表示,我使用 tableview 来显示这些缩略图..,对于每一行都有固定数量的缩略图。在情节提要中,我有一个有 6 个方形视图的单元格,每个视图里面都有一个 imageview。我添加容器视图而不是仅仅放置图像视图的原因是每个缩略图都是可点击的,我需要通过获取 Tapgesture 的视图(容器视图)子视图(即 imageview)标签来知道点击了什么对象。

顺便说一句,我在遇到崩溃的地方发表了评论

4

1 回答 1

0

你应该使用新的 iOS 6 UICollectionView,因为它是为这样的事情而设计的。

于 2012-09-21T06:41:44.243 回答