简体   繁体   English

在uicollectionview中删除选定的单元格

[英]Deleting selected cells in uicollectionview

I have placed a button within a uicollectionviewcell and when that button is pressed, it is programmatically setting the cell as selected. 我在uicollectionviewcell中放置了一个按钮,当按下该按钮时,它以编程方式将单元格设置为选中状态。

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

It then delegates to the uicollectionviewcontroller to deleted the item that is selected. 然后它委托给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];

}

However, when i get the selected items using the uicollectionview method indexPathsForSelectedItems, it is not seeing that I have selected the item and the list is empty. 但是,当我使用uicollectionview方法indexPathsForSelectedItems获取所选项时,它没有看到我已选择该项并且列表为空。 I am using the press to select delegate method for another functionality so I was hoping to do something along the lines of what I explained above. 我正在使用印刷机为另一种功能选择委托方法,所以我希望按照我上面解释的内容做一些事情。 Is there a way to make this work or a better way to inform the controller that the button pressed in the cell was tied to a cell at a specific index path? 有没有办法让这项工作或更好的方式通知控制器按下单元格中的按钮绑定到特定索引路径上的单元格?

Thank you. 谢谢。

Just send a cell pointer from your cell's deleteItem method 只需从单元格的deleteItem方法发送一个单元格指针

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

and change uicollectionviewcontroller's method to 并将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]];

} 

Do not forget to update '(void) deleteItem:(UICollectionViewCell*)cellToDelete' declaration in your *.h file. 不要忘记在* .h文件中更新'(void)deleteItem:(UICollectionViewCell *)cellToDelete'声明。

通过委托方法发送单元格并使用以下方法解决:

    NSIndexPath* indexPath = [self.collectionView indexPathForCell:cell];

animated: 动画:

[self.collectionView performBatchUpdates:^ {
      [_array removeObject:file];            
      [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:nil];

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

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