1
votes

I have created a View Controller with a Navigation bar and UiCollectionView. UI Collection View contains custom UICollectionViewCell. Navigation bar contains two UIBarButton items, one is on the left corner - prepared segue to previous page and other item is on the right corner - arranged to delete cell(s) in the UI CollectionView as show in the picture below:

Main Screen

Now I want to remove the selected UICollectionViewCell when UIBarButtonItem in the right corner, is tapped.

This how my cellForItemAtIndexPath method look like:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
self.GlobalIndexPath = indexPath;
MessagesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"messagesCell" forIndexPath:indexPath];
cell.MessageHeading.text = [self.Message_Heading objectAtIndex:indexPath.row];
cell.MessageSubject.text = [self.Message_Subject objectAtIndex:indexPath.row];
cell.MessageContent.text = [self.Message_Details objectAtIndex:indexPath.row];
[cell.Checkbox setHidden:YES];
[cell.Checkbox setChecked:NO];
}

I have tried a solution like Declaring Indexpath as global variable and use it in the button event as below:

@property (strong,nonatomic) NSIndexPath *GlobalIndexPath;
some other code .......

//When Bin Icon(UIBarButtonItem) Clicked
- (IBAction)DeleteMessages:(id)sender {

[self.view makeToast:@"You clicked delete button !"];

NSIndexPath *indexPath = [self.MessageCollectionView.indexPathsForVisibleItems objectAtIndex:0] ;
BOOL created = YES;
// how to get desired selected cell here to delete
MessagesCollectionViewCell *cell = [self.MessageCollectionView cellForItemAtIndexPath:self.GlobalIndexPath];
if([cell.Checkbox isHidden])
{
    [cell setHidden:YES];
}
else{
    [cell.Checkbox setChecked:NO];
    [cell.Checkbox setHidden:YES];
}
}

It's not worked.

For showing the UICollectionViewCell selected as checked, i'm using @Chris Chris Vasselli's solution

Please help me with this. Thanks in Advance.

1

1 Answers

0
votes

There are a few steps. First, determine the selected indexPath, but don't assume there is a selection when the method is run....

// in your button method
NSArray *selection = [self.MessageCollectionView indexPathsForSelectedItems];
if (selection.count) {
    NSIndexPath *indexPath = selection[0];
    [self removeItemAtIndexPath:indexPath];
}

There are two more steps to remove items from a collection view: remove them from your datasource, and tell the view it has changed.

- (void)removeItemAtIndexPath:(NSIndexPath *)indexPath {
    // if your arrays are mutable...
    [self.Message_Heading removeObjectAtIndex:indexPath.row];

    // OR, if the arrays are immutable
    NSMutableArray *tempMsgHeading = [self.Message_Heading mutableCopy];
    [tempMsgHeading removeObjectAtIndex:indexPath.row];
    self.Message_Heading = tempMsgHeading;
    // ...

Do one or the other above for each datasource array. The last step is to inform the collection view that the datasource has changed, and it must update itself. There are a few ways to do this. The simplest is:

    // ...
    [self.MessageCollectionView reloadData];

OR, a little more elegantly:

    [self.MessageCollectionView deleteItemsAtIndexPaths:@[indexPath]];
}  // end of removeItemAtIndexPath