在编辑按钮上删除UICollectionView单元格 [英] Remove UICollectionView cells on edit button

查看:126
本文介绍了在编辑按钮上删除UICollectionView单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a UICollectionView to retrieve images and labels stored in the Cloud database Parse.

我现在需要一个允许我删除特定图像及其对应标签的选项.

I now need an option that will let me delete a certain image and its corresponding label.

我正在寻找诸如右上角的典型iPhone编辑"按钮之类的东西,该按钮在单元格旁边显示滑动动画和删除按钮.我知道可以通过

I am looking for something such as the typical iPhone "Edit" button on the top right hand corner which displays a swipe animation with a delete button next to the cell. I'm aware that such a thing can be done on a UITableView through

[[self tableView] setEditing:YES animated:YES];

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

but I can't seem to find the equivalent for a UICollectionView anywhere.

任何帮助表示赞赏,即使它不处理从Parse本身进行的删除,也仅是集合视图上的编辑样式才是理想的选择.

Any help appreciated, even if it doesn't deal with the deletion from Parse itself, just the editing style on the collection view would be ideal.

这是我填充细胞的方式:

Here is how I populate my cells:

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

推荐答案

使用大量代码来查看此答案,以供您尝试: https://stackoverflow.com/a/16190291/1914567

Check out this answer with plenty of code for you to try out: https://stackoverflow.com/a/16190291/1914567

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

Basically, there is no Apple-provided way to do this.

也有一些非常不错的库.我个人最喜欢的是 DraggableCollectionView ,还可以查看

There are also some really nice libraries. My personal favorite is DraggableCollectionView, and also check out LXReorderableCollectionViewFlowLayout.

这篇关于在编辑按钮上删除UICollectionView单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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