简体   繁体   English

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

[英]Update Collection View when new cell is added

In my application I capture images one by one,I have a cell at last index which shows "Add more",When new image is added, I want this "Add more" cell to be visible, After 3-4 images "Add more" cell gets hidden. 在我的应用程序中,我一张一张地捕获图像,我在最后一个索引中显示了“添加更多”单元,添加新图像后,我希望此“添加更多”单元可见,在3-4张图像之后“添加更多” ”单元格被隐藏了。 How can I update collection view such that "Add more" cell is always visible. 如何更新收藏夹视图,以使“添加更多”单元格始终可见。 Thank You :) 谢谢 :)

Code to add Images : 添加图像的代码:

- (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];
    }

}

Add below method and call it where you are adding new cell. 添加以下方法,并在您要添加新单元格的地方调用它。

-(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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM