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