删除集合视图中的选定项目

时间:2015-01-19 11:09:32

标签: ios

我在集合视图中选择了5个项目,我想将其删除。请帮我删除所选项目。我放了一个按钮,当我按下那个按钮时,所有选中的项目都删除了我想要的。我的按钮点击代码。

- (IBAction)btn_delete:(id)sender {
    NSArray *selectedItemsIndexPaths = [self.MyCollectionView indexPathsForSelectedItems];
    NSLog(@"Selected images: %@",mySelectedArray);
    [self.MyCollectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];
    [self.MyCollectionView reloadData];
}

3 个答案:

答案 0 :(得分:2)

首先删除Objects for DataSource,然后尝试从Collectionview中删除单元格

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

            [self.collectionView performBatchUpdates:^{

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

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

            } completion:nil];
        }
}

答案 1 :(得分: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

答案 2 :(得分:0)

在你的btn_delete:方法中,添加此代码

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

在collectionView:didSelectItemAtIndexPath:方法中,首先应该在添加到数组之前检查数组是否已包含该索引路径。 你可以在下面看到它:

 if(![mySelectedArray containsObject:indexPath])
    [mySelectedArray addObject:indexPath];