集合视图diddeselect更新不起作用并崩溃

时间:2018-06-28 23:14:54

标签: ios objective-c uicollectionview nsarray

我在视图控制器上有两个集合视图。 照片收藏视图-填充图库中的所有图像。 SelectedPhotosCollectionView-在照片收藏视图中用选定的图像填充单元格。

对于填充单元格,我已经在cellforrowatindexpath中完成了以下代码。

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = (cv == _selectedPhotosCollectionView)?  @"selectedPhoto" : @"imagePickerCell";
    ImagePickerCustomCell *cell = (ImagePickerCustomCell *)[cv dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cv== _photosCollectionView){

        UIImage *thumbnail = [imageArray objectAtIndex:indexPath.row];
        cell.photosImageView.frame = AVMakeRectWithAspectRatioInsideRect(CGSizeMake(400,400), cell.photosImageView.bounds);
        cell.photosImageView .image = thumbnail;
        cell.photosImageView.clipsToBounds = YES;
        cell.photosImageView.contentMode = UIViewContentModeScaleAspectFill;
        if (cell.selected) {
            [cell.selectedImageView setHidden:NO];
        } else{
            [cell.selectedImageView setHidden:YES];
        }
        }
    else{


            NSDictionary *photoDict = [_collage.selectedPhotos objectAtIndex:indexPath.row];
            dispatch_async(dispatch_get_main_queue(), ^{
                _selectedImagesCount.text = [NSString stringWithFormat:@"%lu",(unsigned long)[_collage.selectedPhotos count]];
            });
            id i = [photoDict objectForKey:@"smallImage"];
            if ([i isKindOfClass:[NSData class]]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    cell.imageSelected.image = [UIImage imageWithData:(NSData *) i];
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{

                    cell.imageSelected.image = (UIImage *) i;
                });
            }

    }

    return cell;
}

要获取行数:-

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == _selectedPhotosCollectionView) {
        NSLog(@"numberOfItemsInSection print the count of _selectedPhotosCollectionView %lu",(unsigned long)[_collage.selectedPhotos count]);
        return [_collage.selectedPhotos count];
    } else if (collectionView == _photosCollectionView){
         NSLog(@"numberOfItemsInSection print the count of _selectedPhotosCollectionView %lu",(unsigned long)[imageArray count]);
        return [imageArray count];
    } else return 1;
}

供选择:-

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    ImagePickerCustomCell *cell =  (ImagePickerCustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{

        if (collectionView == _photosCollectionView){
            dispatch_sync(dispatch_get_main_queue(), ^{
                [cell.selectedImageView setHidden:NO];
            });
            UIImage *assetPhoto = [imageArray objectAtIndex:indexPath.row];
            [selectedImageArray addObject:assetPhoto];
            if([selectedImageArray count]>6){
                 dispatch_async(dispatch_get_main_queue(), ^{
                [_nextBtn setEnabled:NO];
                [[HelperClass sharedHelperClass]showAlert:@"" message:@"Please Select 6 Photos Only" viewController:self];
                     });
                //collectionView.allowsSelection = false;
            }else{
                dispatch_sync(dispatch_get_main_queue(), ^{
                [_nextBtn setEnabled:YES];
                    });
                // collectionView.allowsSelection = true;

            }
            NSData *data = UIImagePNGRepresentation(assetPhoto);
            photoDictionary = @{@"info": [NSNull null], @"smallImage": data};
            NSInteger index = [_collage.selectedPhotos count];
            NSArray *arrayWithIndexPaths = @[[NSIndexPath indexPathForRow:index inSection:0]];
            dispatch_sync(dispatch_get_main_queue(), ^{
                 NSLog(@"didselect print the count of _selectedPhotosCollectionView %lu",(unsigned long)[_collage.selectedPhotos count]);
                [_collage.selectedPhotos addObject:photoDictionary];

                [_selectedPhotosCollectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
                NSLog(@"didselect print the count of _selectedPhotosCollectionView %lu",(unsigned long)[imageArray count]);
            });
        }else{

        }
    });

}

取消选择:-

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    ImagePickerCustomCell *cell =  (ImagePickerCustomCell *) [collectionView  cellForItemAtIndexPath:indexPath];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{
        if (collectionView == _photosCollectionView) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                [cell.selectedImageView setHidden:YES];
            });

            UIImage *assetPhoto = [imageArray objectAtIndex:indexPath.row];
            dispatch_sync(dispatch_get_main_queue(), ^{

                [selectedImageArray removeObject:assetPhoto];
            });
            if([selectedImageArray count]>6){
                dispatch_async(dispatch_get_main_queue(), ^{

                    [_nextBtn setEnabled:NO];
                    [[HelperClass sharedHelperClass]showAlert:@"" message:@"Please Select 6 Photos Only" viewController:self];
                });
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{

                    [_nextBtn setEnabled:YES];
                });
            }
            dispatch_async(dispatch_get_main_queue(), ^{

                [_selectedImagesCount setText:[NSString stringWithFormat:@"%lu",(unsigned long)[selectedImageArray count]] ];
            });
            NSData *data = UIImagePNGRepresentation(assetPhoto);
            NSDictionary *photoDictionary = @{@"info": [NSNull null], @"smallImage": data};
            NSInteger index = [_collage.selectedPhotos indexOfObject:photoDictionary];
           // int index = (int)indexValue;
           // NSLog(@"get values of index here %d",index);

            //if(index>0){
            NSArray *arrayWithIndexPaths = @[[NSIndexPath indexPathForRow:index inSection:0]];
            dispatch_sync(dispatch_get_main_queue(), ^{
                NSLog(@"diddeselect print the count of _selectedPhotosCollectionView %lu",(unsigned long)[_collage.selectedPhotos count]);
                [_collage.selectedPhotos removeObject:photoDictionary];

                [_selectedPhotosCollectionView deleteItemsAtIndexPaths:arrayWithIndexPaths];
                NSLog(@"diddeselect print the count of _selectedPhotosCollectionView %lu",(unsigned long)[imageArray count]);
            });
           // }
        }else{
        }
    });
}

问题-单元格的选择工作得很好,但是取消选择会崩溃并显示异常

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).

我是ios开发的新手,已经走了很多路,发现了许多与我类似的问题,但无法获得我的情况的帮助。 请帮助我解决这个问题。谢谢!

0 个答案:

没有答案