8

我正在构建我的第一个应用程序,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很感兴趣- 我是否必须实现自己的删除手势/控件,或者我是否缺少某些东西(或开源)?

4

4 回答 4

7

我将此代码插入到包含 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];
}
}
于 2013-05-01T02:29:50.270 回答
6

默认 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
}
于 2013-03-26T01:33:55.413 回答
0

对于将来阅读本文的任何人,这是我一直在使用的一个有用的库: 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

希望有帮助!

于 2014-11-08T20:04:19.800 回答
0

我在 UICollectionView 中发现并正在实施的删除和编辑内容来自此博客文章。

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

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

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

于 2013-04-22T22:31:43.523 回答