简体   繁体   English

在点击UIBarButtonItem时从indexPath获取UICollectionViewCell

[英]Get UICollectionViewCell from indexPath when UIBarButtonItem is tapped

I have created a View Controller with a Navigation bar and UiCollectionView. 我创建了一个带有导航栏和UiCollectionView的视图控制器。 UI Collection View contains custom UICollectionViewCell. UI集合视图包含自定义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: 导航栏包含两个UIBarButton项目,一个位于左上角 - 准备了segue到上一页,其他项目位于右上角 - 用于删除UI CollectionView中的单元格,如下图所示:

Main Screen 主屏幕

Now I want to remove the selected UICollectionViewCell when UIBarButtonItem in the right corner, is tapped. 现在,我想要在右上角的UIBarButtonItem被轻击时删除所选的UICollectionViewCell。

This how my cellForItemAtIndexPath method look like: 这是我的cellForItemAtIndexPath方法的样子:

-(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: 我尝试过将Decpathing Indexpath作为全局变量的解决方案,并在按钮事件中使用它,如下所示:

@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 为了显示选中的UICollectionViewCell,我正在使用@Chris Chris Vasselli的解决方案

Please help me with this. 请帮我解决一下这个。 Thanks in Advance. 提前致谢。

There are a few steps. 有几个步骤。 First, determine the selected indexPath, but don't assume there is a selection when the method is run.... 首先,确定所选的indexPath,但不要假设在运行方法时有选择....

// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM