【问题标题】:Delete selected Items in collection view删除集合视图中的选定项目
【发布时间】:2015-01-19 11:09:31
【问题描述】:

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

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

【问题讨论】:

  • 使用此代码我无法删除。但是当我打印“mySelectedArray”时,它会显示我所有选择的图像。请帮助
  • 您必须删除您的 collectionView 数据示例中的项目:[self.MyCollectionViewDataArray removeObjectsAtIndexes :selectedItemsIndexes];然后重新加载数据

标签: ios


【解决方案1】:

首先删除数据源的对象,然后尝试从 Collectionview 中删除单元格

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

            [self.collectionView performBatchUpdates:^{

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

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

            } completion:nil];
        }
}

【讨论】:

    【解决方案2】:
    #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
    

    【讨论】:

    • 看看这是我的“viewcontroller.m”代码。我想在单击按钮时删除选定的项目,但我无法删除。在这里,我清楚我是选择图像并且选择的图像对象添加到新数组中。我在我的程序里写什么代码????
    【解决方案3】:

    在您的 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];
    

    【讨论】:

    • 如果你喜欢我的回答并帮助了你,你可以点赞
    猜你喜欢
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    相关资源
    最近更新 更多