ios - 删除 uicollectionview 中的选定单元格

标签 ios objective-c uicollectionview uicollectionviewcell

我在 uicollectionviewcell 中放置了一个按钮,当按下该按钮时,它以编程方式将单元格设置为选中状态。

- (void) deleteItem:(id)sender
{
    self.selected = YES;
    [self.cellOptionsDelegate deleteItem];
}

然后它委托(delegate) uicollectionviewcontroller 删除选中的项目。

- (void) deleteItem
{
    NSArray* selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems];

    // Delete the items from the data source.
    [self.taskArray removeObjectAtIndex:[[selectedItemsIndexPaths objectAtIndex:0] row]];

    // Now delete the items from the collection view.
    [self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];

}

但是,当我使用 uicollectionview 方法 indexPathsForSelectedItems 获取所选项目时,它没有看到我已经选择了该项目并且列表为空。我正在使用新闻来为另一个功能选择委托(delegate)方法,所以我希望按照我上面解释的内容做一些事情。有没有办法使这项工作或更好的方法来通知 Controller 单元格中按下的按钮已绑定(bind)到特定索引路径中的单元格?

谢谢。

最佳答案

只需从单元格的 deleteItem 方法发送一个单元格指针

- (void) deleteItem:(id)sender
{
    self.selected = YES;
    [self.cellOptionsDelegate deleteItem:self];
}

并将 uicollectionviewcontroller 的方法更改为

- (void) deleteItem:(UICollectionViewCell*)cellToDelete
{
    NSIndexPath indexPathToDelete = [self indexPathForCell: cellToDelete];

    // Delete the items from the data source.
    [self.taskArray removeObjectAtIndex:[indexPathToDelete row]];

    // Now delete the items from the collection view.
    [self.collectionView deleteItemsAtIndexPaths:@[indexPathToDelete]];

} 

不要忘记更新 *.h 文件中的 '(void) deleteItem:(UICollectionViewCell*)cellToDelete' 声明。

关于ios - 删除 uicollectionview 中的选定单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23095797/

相关文章:

python - 如何为要发送 POST 请求的 iPhone 和 Android 应用程序生成 Django CSRF key ?

android - 钛手机 : Slide to switch between the views

ios - 重新加载 TableView 中除第一个单元格之外的所有行的最有效方法是什么?

iphone - 如何以编程方式区分 iphone 4 和 iphone 4S?

ios - NSMutableArray 无法插入字典

swift - 为什么我的 UICollectionViewController 不显示任何内容?

ios - 多个 UICollectionView

ios - CAShapeLayer 改变不同画笔的宽度

iphone - 如果移动,UIImageView 将缩放而不是旋转,iOS

ios - UICollectionView 仅显示一个单元格 - 当 View 滚动时会发生变化