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

时间:2015-02-26 17:03:12

标签: objective-c uitableview parse-platform uicollectionview uicollectionviewcell

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

我现在需要一个允许我删除某个图像及其相应标签的选项。

我正在寻找一些东西,例如右上角的典型iPhone“编辑”按钮,它会在单元格旁边显示一个带有删除按钮的滑动动画。我知道可以通过

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

2 个答案:

答案 0 :(得分:3)

请使用大量代码查看此答案:https://stackoverflow.com/a/16190291/1914567

基本上,没有Apple提供的方法来做到这一点。

还有一些非常好的图书馆。我个人最喜欢的是DraggableCollectionView,还可以查看LXReorderableCollectionViewFlowLayout

答案 1 :(得分:-3)

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