Remove UICollectionView cells on edit button

柔情痞子 提交于 2019-12-07 13:37:05

问题


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.

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

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

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

回答1:


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

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

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




回答2:


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


来源:https://stackoverflow.com/questions/28748393/remove-uicollectionview-cells-on-edit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!