删除编辑按钮上的 UICollectionView 单元格

Remove UICollectionView cells on edit button

我正在使用 UICollectionView 检索存储在云数据库 Parse 中的图像和标签。

我现在需要一个选项,可以让我删除某个图像及其对应的标签。

我正在寻找诸如右上角典型的 iPhone "Edit" 按钮之类的东西,它在单元格旁边显示带有删除按钮的滑动动画。我知道这样的事情可以通过

在 UITableView 上完成
[[self tableView] setEditing:YES animated:YES];

但我似乎无法在任何地方找到 UICollectionView 的等效项。

任何帮助表示赞赏,即使它不处理从 Parse 本身删除,只是集合视图上的编辑样式将是理想的。

这是我填充单元格的方式:

- (void)viewDidLoad
{
   [super viewDidLoad];
   [self retrieveSelectedImages];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [imageFilesArray count];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"myRoomsCell";
MyRoomsCell *cell = (MyRoomsCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

PFObject *imageObject = [imageFilesArray objectAtIndex:indexPath.row];
PFFile *imageFile = [imageObject objectForKey:@"imageFile"];

cell.loadingSpinner.hidden = NO; //show loading spinner to indicate work is happening until the image loads
[cell.loadingSpinner startAnimating];

// UILabel *label = (UILabel*) [cell viewWithTag:5];
cell.label.text= [imageObject objectForKey:@"roomLabel"]; //set room label as the label stored on parse previously inserted by the user

cell.label.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
cell.label.textAlignment = NSTextAlignmentCenter;


[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
{
    if (!error) {
        cell.parseImage.image = [UIImage imageWithData:data];
        [cell.loadingSpinner stopAnimating];
        cell.loadingSpinner.hidden = YES;
    }
}];

return cell;
}

-(void) retrieveSelectedImages
{
//parse query where we search the favorites array column and return any entry where the array contains the logged in user objectid
PFQuery *getFavorites = [PFQuery queryWithClassName:@"collectionViewData"];
[getFavorites whereKey:@"selectedImage" equalTo:[PFUser currentUser].objectId];

[getFavorites findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error)
    {
        imageFilesArray = [[NSArray alloc] initWithArray:objects];
        [roomsCollection reloadData];
    }
}];
}

查看此答案,其中包含大量代码供您试用:

基本上,Apple 没有提供执行此操作的方法。

还有一些非常好的库。我个人最喜欢的是 DraggableCollectionView, and also check out LXReorderableCollectionViewFlowLayout.

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[Singleton sharedSingleton].selectedProfileArray removeObjectAtIndex:indexPath.row];
    [selectCollectionView reloadData];
}