UICollectionView如何删除单元格(相当于commitEditingStyle)?

Posted

技术标签:

【中文标题】UICollectionView如何删除单元格(相当于commitEditingStyle)?【英文标题】:UICollectionView how to delete cells (equivalent of commitEditingStyle)? 【发布时间】:2013-03-25 17:00:48 【问题描述】:

我正在使用UICollectionView 构建我的第一个应用程序,并注意到在删除对象方面我无能为力。对于UITableView 应用,有滑动删除方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

if (editingStyle == UITableViewCellEditingStyleDelete) 
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
     


当我使用GMGridView时,它的行为类似于在iPhone主屏幕上长按-摇动视图星星并显示一个删除按钮,该按钮负责删除视图。我当然可以尝试复制这种行为,但不确定用户是否会“得到它”。

我对 我有哪些选项可以让用户从 UICollectionView 删除对象感兴趣 - 我是否必须实现自己的删除手势/控件,或者有什么我想要的缺少(或开源)?

【问题讨论】:

使用 UICollectionView 的次数越多,我就越意识到 UITableView 的成熟度... 【参考方案1】:

我在包含 CollectionView 的视图控制器中插入了这段代码,并这样做了。您可能已经在使用点击手势执行类似的操作来检测选定的单元格。

- (IBAction)didLongPressCellToDelete:(UILongPressGestureRecognizer*)gesture 
CGPoint tapLocation = [gesture locationInView:self.myCollectionView];
NSIndexPath *indexPath = [self.myCollectionView indexPathForItemAtPoint:tapLocation];
if (indexPath && gesture.state == UIGestureRecognizerStateBegan) 
    NSLog(@"image with index %d to be deleted", indexPath.item);
    self.itemToBeDeleted = indexPath.item;
    UIAlertView *deleteAlert = [[UIAlertView alloc]
                             initWithTitle:@"Delete?"
                             message:@"Are you sure you want to delete this image?"
                             delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
    [deleteAlert show];





- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
NSLog(@"selected button index = %d", buttonIndex);
if (buttonIndex == 1) 
    // Do what you need to do to delete the cell 
    [self.myCollectionView reloadData];


【讨论】:

这是什么self.itemToBeDeleted self.itemToBeDeleted 设置为用户要删除的项目的索引。 明白了! :) @RawMean【参考方案2】:

默认 UICollectionViewCell 只有一个空白视图(没有标题、没有删除按钮、没有 imageView)

子类 UICollectionViewCell 并在其上添加删除按钮。 setHidden = NO 当你想显示它时(例如向下滑动)

使用自定义委托删除数据并重新加载collectionView

示例使用向右滑动删除 UICollectionViewCell 在 storyBoard 中垂直滚动:

//Cell.h
@class MyCell;
@protocol MyCellDelegate
-(void)deleteButtonClick:(MyCell *)cell;
@end
@interface MyCell : UICollectionViewCell
@property (weak , nonatomic) id<MyCellDelegate> delegate;
@property (weak,nonatomic) IBOutlet *delButton;
@end

//Cell.m
-(void)awakeFromNib
  [self.delButton setHidden:YES]
  //add Swipe right to here

-(void)handleSwipeRight:(UISwipeGestureRecognizer *)swipe 
   [self.delButton setHidden:NO];

-(IBAction)clickDelBut
 [self.delegate deleteButtonClick:self];


//ViewController.m
//In cellForItemsAtIndexPath cell.delegate = self.

-(void)deleteButtonClick:(MyCell *)cell
  //get indexPath, delete data and reload collectionView here

【讨论】:

我担心我的收藏视图垂直滚动,所以向下滑动不起作用。也许这就是为什么没有滑动删除的原因 - 因为教育用户向某个方向滑动或长按太麻烦了。【参考方案3】:

我在 UICollectionView 中发现和正在实施的删除和编辑内容来自这篇博文。

基本上显示复制/粘贴等菜单,并添加我自己的删除操作。它类似于 iSang 的答案,但没有添加在 UICollectionViews 中效果不佳的手势识别器。

当人们想要在 ios 的其他部分复制和粘贴文本和链接时,它使用长按手势来调出编辑菜单。

http://paulsolt.com/2012/11/uicollectionview-custom-actions-and-uimenucontroller/

【讨论】:

【参考方案4】:

对于将来阅读本文的任何人,这是我一直在使用的一个有用的库: https://github.com/Raizlabs/RZUtils/tree/master/RZUtils/Components/RZCollectionTableView

它使用 UICollectionView 模仿 UITableView,因此您可以获得集合视图的灵活性以及 UITableView 附带的所有不错的编辑功能。

但是,如果您尝试实现不同类型的删除行为或不同的布局,据我所知,您必须从头开始实现它。如果您不熟悉使用 UIGestureRecognizers 进行自定义操作,我会推荐本教程: http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

希望有帮助!

【讨论】:

以上是关于UICollectionView如何删除单元格(相当于commitEditingStyle)?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 UICollectionView 中正确删除单元格?

如何在 UICollectionView 拖放期间删除“幽灵”单元格,并使移动单元格不透明?

如何删除 UICollectionView 单元格中的单个对象?

UICollectionView如何删除单元格(相当于commitEditingStyle)?

如何使用 MVVM 和 RxSwift 编辑/删除 UICollectionView 单元格

如何删除 UICollectionView Flow 布局大小中的单元格空间?