Удалить ячейки UICollectionView на кнопке редактирования

Я использую UICollectionView для извлечения изображений и меток, хранящихся в облачной базе данных 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];
    }
}];
}



Ответы (1)


Ознакомьтесь с этим ответом с большим количеством кода, который вы можете попробовать: https://stackoverflow.com/a/16190291/1914567

По сути, у Apple нет способа сделать это.

Есть также несколько действительно хороших библиотек. Мой личный фаворит — DraggableCollectionView, а также проверьте LXReorderableCollectionViewFlowLayout.

person st.derrick    schedule 26.02.2015