-2

I select 5 items in my collection view and i want to delete it. Please help me delete the selected items. I put one button and when i press that button all selected items are delete that i want. My button click code.

- (IBAction)btn_delete:(id)sender {
    NSArray *selectedItemsIndexPaths = [self.MyCollectionView indexPathsForSelectedItems];
    NSLog(@"Selected images: %@",mySelectedArray);
    [self.MyCollectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];
    [self.MyCollectionView reloadData];
}
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
Parth Patel
  • 55
  • 1
  • 6
  • With this code i can't delete. But when i print "mySelectedArray" it is show me all my selected images. Plz help – Parth Patel Jan 19 '15 at 11:12
  • you must delete items in your collectionView data example: [self.MyCollectionViewDataArray removeObjectsAtIndexes :selectedItemsIndexes]; then reloadData – Huy Nghia Jan 19 '15 at 11:13
  • http://stackoverflow.com/questions/16296351/how-to-delete-an-item-from-uicollectionview-with-indexpath-row – Sport Jan 19 '15 at 11:17

3 Answers3

3

First remove Objects for DataSource, then try delete cells from Collectionview

-(void)deleteCellInCollectionViewAtIndex:(int)index{  
  if (self.collectionView) {

            [self.collectionView performBatchUpdates:^{

                NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:index inSection:0] ;

                [self.collectionView deleteItemsAtIndexPaths:@[cellIndexPath]];

            } completion:nil];
        }
}
cdon
  • 264
  • 1
  • 13
0
#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController

#pragma Mark For Collection View Methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

   return [patternImageArray count]; 
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *identifier = @"Cell";
   PatternViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

   NSString *myPatternString = [patternImageArray objectAtIndex:indexPath.row];

   cell.ImageView.image = [UIImage imageNamed:myPatternString];
   cell.ImageLabel.text = myPatternString;
   ind = indexPath.row;
   return cell; }

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

    PatternViewCell *mySelectedCell = (PatternViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    mySelectedCell.ImageLabel.backgroundColor = [UIColor blueColor];
    mySelectedCell.ImageLabel.textColor = [UIColor whiteColor];
   [mySelectedArray addObject:indexPath];
    i = indexPath.row;  }

- (void)viewDidLoad  {

   [super viewDidLoad];
   patternImageArray = [[NSMutableArray alloc]initWithObjects:@"Thar.jpg",@"Thar1.jpg",@"Thar2.jpg",@"Thar3.jpg",@"Thar4.jpg",@"Thar5.jpg",@"Thar6.jpg",@"Thar7.jpg",@"Thar8.jpg", nil];
   self.MyCollectionView.multipleTouchEnabled = YES;
   mySelectedArray = [[NSMutableArray alloc]init];   }

- (IBAction)btn_delete:(id)sender 
{
   [self.MyCollectionView reloadData];
   NSLog(@"Selected images: %@",mySelectedArray); 
 }
@end
Parth Patel
  • 55
  • 1
  • 6
  • See this is my "viewcontroller.m" Code. I want to delete selected items on click the button,but i can't delete. Here I clear that I am select images and that selected images objects add in new array. What I write a code in my program???? – Parth Patel Jan 19 '15 at 13:15
0

In your btn_delete: method, add this code

NSArray *selectedItemsIndexPaths = [self.MyCollectionView indexPathsForSelectedItems];
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
for (NSInteger counter = 0; counter < [mySelectedArray count]; counter++) {
    NSIndexPath *indexPath = mySelectedArray[counter];
    [indexSet addIndex:indexPath.row];
}
[patternImageArray removeObjectsAtIndexes:indexSet];
[self.collectionView performBatchUpdates:^{

    [self.collectionView deleteItemsAtIndexPaths:mySelectedArray];

} completion:^(BOOL finished) {
    [mySelectedArray removeAllObjects];
}];

In your collectionView:didSelectItemAtIndexPath: method, you should first check if array already contains that indexpath before adding to array. You can see it below:

 if(![mySelectedArray containsObject:indexPath])
    [mySelectedArray addObject:indexPath];
Tejvansh
  • 676
  • 4
  • 9