【问题标题】:Get UICollectionViewCell from indexPath when UIBarButtonItem is tapped点击 UIBarButtonItem 时从 indexPath 获取 UICollectionViewCell
【发布时间】:2017-07-29 03:40:40
【问题描述】:

我创建了一个带有导航栏和 UiCollectionView 的视图控制器。 UI Collection View 包含自定义 UICollectionViewCell。导航栏包含两个 UIBarButton 项,一个在左角 - 准备转到上一页,另一个在右上角 - 安排删除 UI CollectionView 中的单元格,如下图所示:

Main Screen

现在我想在点击右上角的 UIBarButtonItem 时删除选定的 UICollectionViewCell。

这就是我的 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];
}

我尝试过像 Declaring Indexpath as global variable 之类的解决方案,并在按钮事件中使用它,如下所示:

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

没用。

为了显示选中的 UICollectionViewCell,我使用的是@Chris Chris Vasselli's solution

请帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell uibarbuttonitem


    【解决方案1】:

    有几个步骤。首先确定选择的indexPath,但不要假设运行方法时有选择....

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

    还有两个步骤可以从集合视图中删除项目:从数据源中删除它们,并告诉视图它已更改。

    - (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;
        // ...
    

    对每个数据源数组执行上述操作。最后一步是通知集合视图数据源已更改,它必须自行更新。有几种方法可以做到这一点。最简单的是:

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

    或者,更优雅一点:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多