添加新单元格时更新集合视图



在我的应用程序中,我一个接一个地捕获图像,我在最后一个索引处有一个单元格显示"添加更多",当添加新图像时,我希望这个"添加更多"单元格可见,在 3-4 张图像后"添加更多"单元格被隐藏。如何更新集合视图,使"添加更多"单元格始终可见。谢谢:)

添加图像的代码:

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

if (indexPath.row == [imageArray count]) {
    cell.ImageImg.image =[UIImage imageNamed:@"plus.jpg"];
    cell.DeleteBtn.hidden=true;
    [cell.ImageImg.layer setBorderColor: [[UIColor clearColor] CGColor]];
    [cell.ImageImg.layer setBorderWidth: 0.5];
}
else
{
    cell.DeleteBtn.hidden=false;
    cell.ImageImg.image=[imageArray objectAtIndex:indexPath.row];
    [cell.ImageImg.layer setBorderColor: [[UIColor blackColor] CGColor]];
    [cell.ImageImg.layer setBorderWidth: 0.7];
    [cell.DeleteBtn addTarget:self action:@selector(RemovePrssd:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(void)RemovePrssd:(id)sender{
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [_ImageCollectionVIew indexPathForCell: (UICollectionViewCell *)[[senderButton superview]superview]];
[imageArray removeObjectAtIndex:indexPath.row];
[_ImageCollectionVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  {
    if (indexPath.row == [imageArray count]) {
        value=@"0";
        [_videoController.view removeFromSuperview ];
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:NULL];
    }
}

添加下面的方法并在添加新单元格的位置调用它。

-(void)scrollToBottom
{
CGFloat collectionViewContentHeight = _ImageCollectionVIew.contentSize.height;
CGFloat collectionViewFrameHeightAfterInserts = _ImageCollectionVIew.frame.size.height - (_ImageCollectionVIew.contentInset.top + _ImageCollectionVIew.contentInset.bottom);
if(collectionViewContentHeight > collectionViewFrameHeightAfterInserts) {
    [_ImageCollectionVIew setContentOffset:CGPointMake(0,_ImageCollectionVIew.contentSize.height - _ImageCollectionVIew.frame.size.height) animated:NO];
    }
}

相关内容

  • 没有找到相关文章