sermilion
sermilion

Reputation: 185

How delete row form UICollectionView?

I have UICollectionView that has two UICollectionResuabeView(Custom View) and one UICollectionViewCell. The form a single entity in UICollectionView. I put Delete button on one of UICollectionResuabeView and by pressing it want to delete the row (if i call it right). This is the screenshot form Storyboard: enter image description here

I tried getting section of it my many means, but non works. Guess I just dont have enough knowledge of how UICollectionView works. Here is some code I tried: I know its I mess, but at least I was trying ))

I ve tried this toget indexPath, but none works. -(IBAction)deletePressed:(UIButton* )sender{

    RecipeCollectionHeaderView *contentView = (RecipeCollectionHeaderView *)[sender superview];
    RecipeViewCell *cell = (RecipeViewCell *)[contentView superview];
    cell = (RecipeViewCell *)[contentView superview];

    // determine indexpath for a specific cell in a uicollectionview
    NSIndexPath *editPath = [self.collectionView indexPathForCell:cell];
    NSInteger rowIndex = editPath.row;
    NSInteger secIndex = editPath.section;
//    NSIndexPath *indexPath = nil;
//    indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:sender.center fromView:sender.superview]];
//    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
//    NSSet *touches = [event allTouches];
//    
//    UITouch *touch = [touches anyObject];
//    
//    CGPoint currentTouchPosition = [touch locationInView:self.collectionView];
//    
//    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint: currentTouchPosition];
    NSLog(@"IndexPath CollectionView %ld", (long)rowIndex);

}

I also tried make it work like in this post, but it does not give a result:how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView Thank you all!

EDIT! I found this works well:

-(IBAction)deletePressed:(UIButton* )sender{
    [self.ordersArray removeObjectAtIndex:0];
    [self.collectionView reloadData];
}

The only thing I need now is to get section of UICollectionView by UIBotton that been pressed, to delete section I want. How to do it?)

enter image description here

Upvotes: 0

Views: 1056

Answers (1)

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

You also need to remove item from the array and UICollectionView as well.

Try to use this code.

  -(void) deleteRow:(NSInteger)index {

        [self.contentView performBatchUpdates:^{
            [arrayForRows removeObjectAtIndex:i];
            NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
            [self.contentView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

        } completion:^(BOOL finished) {

        }];
    }

For cell selection use this code snippet:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Item position that you would like to delete.
    indexPath.row;  
}

Upvotes: 3

Related Questions