我需要一种方法来删除imageview mockCell中的单元格。任何提示或建议将不胜感激。
//Create mock cell to drag around
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.highlighted = NO;
[mockCell removeFromSuperview];
mockCell = [[UIImageView alloc] initWithFrame:cell.frame];
mockCell.image = [self imageFromCell:cell];
mockCenter = mockCell.center;
[self.collectionView addSubview:mockCell];
[UIView
animateWithDuration:0.3
animations:^{
mockCell.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
}
completion:nil];
答案 0 :(得分:0)
我认为您正在寻找的是:
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
执行此操作时,还应从数据源中删除它:保持DataSource和CollectionView保持同步。
答案 1 :(得分:0)
您可以使用[self.collectionView indexPathsForSelectedItems]; 我这样做是为了一次删除多张图片。
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == deletePhotoConfirmAlert) {
if (buttonIndex == 1) {
// Permission to delete the button is granted here.
NSArray *selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems];
// Delete the items from the data source.
[self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths];
// Now delete the items from the collection view.
[self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];
}
}
}
// This method is for deleting the selected images from the data source array
-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths
{
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSIndexPath *itemPath in itemPaths) {
[indexSet addIndex:itemPath.row];
}
[self.images removeObjectsAtIndexes:indexSet]; // self.images is my data source
}
如果您使用的是StoryBoard,请编辑此方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems];
DetailViewController *dest = [segue destinationViewController];
dest.imageName = [self.images objectAtIndex:[[indexpaths objectAtIndex:0] row]];
}