UICollectionView如何删除单元格(相当于commitEditingStyle)? [英] UICollectionView how to delete cells (equivalent of commitEditingStyle)?

查看:176
本文介绍了UICollectionView如何删除单元格(相当于commitEditingStyle)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building my first application using UICollectionView and noticed that there isn't much I can do in terms of object deletion. For UITableView apps, there's the swipe to delete method:

- (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主屏幕的行为 - 可以显示视图星星并且可以显示删除按钮,该按钮负责删除视图。我当然可以尝试复制这种行为,但我不确定用户是否会得到它。

When I use GMGridView, it has behaviors similar to long press on the iPhone home screen - the view stars to shake and a delete button can be displayed, which is responsible for deleting the view. I can certainly try to replicate this behavior, but am not sure if users will "get it".

我对允许用户从 UICollectionView 中删除​​对象的选项感兴趣 - 我是否必须实现自己的删除手势/控件,或者是否有我缺少的东西(或开源)?

I'm interested in what are my options for letting the user delete objects from UICollectionView - do I have to implement my own delete gestures/controls, or is there something that I'm missing (or open source)?

推荐答案

我在包含CollectionView的视图控制器中插入了此代码并以此方式执行。您可能已经使用点按手势来检测所选单元格。

I inserted this code in my view controller that includes the CollectionView and did it this way. You are probably already doing something like this with the tap gesture to detect selected cell.

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

这篇关于UICollectionView如何删除单元格(相当于commitEditingStyle)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆