UICollectionView:在insertItemsAtIndexPaths之后未调用cellForItemIndexPath

时间:2014-05-04 18:03:54

标签: objective-c uicollectionview uicollectionviewcell ios7.1

我有一个显示UIImages的UICollectionView。初始加载后,集合视图正确显示self.image rray(数据源)中的图像。委托和dataSource直接在故事板中设置。

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageViewCell" forIndexPath:indexPath];

    UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];
    imgView.image = [self.images objectAtIndex:indexPath.item];
    return cell;
}

添加新图片后,更新无效。不调用方法cellForItemIndexPath。任何的想法?以下代码添加了新图像。 self.imgPhoto正确显示所选图像。因此,使用UICollectionView的内容应该是错误的。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    self.imgPhoto.image = chosenImage;
    [self.images addObject:chosenImage];
    [self.cvPhotos insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.images.count-1 inSection:0]]];

    NSLog(@"didFinishPickingMediaWithInfo: insert: %lu %d", self.images.count-1, 0);

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

1 个答案:

答案 0 :(得分:0)

我发现了这个错误。由于某种原因,故事板失去了与cvPhotos的链接。总是零。因此,未调用insertItemAtIndexPath方法。

但我没有收到任何错误消息。当我调用nil-object的方法时,这是正确的吗?