0

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

sermilion
  • 185
  • 1
  • 15

1 Answers1

3

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;  
}
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • Hi, thank you for reply. Seems like good answer. But I dont have self.contentView property. I know this is probably something silly, but could you walk me through this?) Thank you. – sermilion Feb 23 '15 at 08:06
  • self.contentView it is you're UiCollection view – Oleg Gordiichuk Feb 23 '15 at 08:08
  • Just a little pet peeve, it's not `UiCollection` its `UICollectionView` remember a lot of beginners rely on this information. Don't lead them down the wrong path. – soulshined Feb 23 '15 at 08:19
  • I need to find how to know section, by the button pressed? That would solve my problem. Thanks) – sermilion Feb 23 '15 at 11:26
  • You need to index of the cell that you select ? – Oleg Gordiichuk Feb 23 '15 at 12:02
  • I guess I am getting annoying and probably I am not expressing myself right, sorry)) What you suggested gives indexPath for each item in Collection cell. What I would like to get, I guess, would be section index, as I thought right now) When I click Delete button. See picture I just uploaded. Many thanx) – sermilion Feb 23 '15 at 13:02