gpt4 book ai didi

ios - 如何在UICollectionView中获取选定的CollectionViewCell?

转载 作者:行者123 更新时间:2023-12-01 18:58:54 25 4
gpt4 key购买 nike

我是IOS开发的新手,我尝试使用UIcollectionView显示照片而不使用storyboard

我创建一个xib file调用AITFileCell_grid.xib,并添加一个Collection View Cell

我还创建了另一个xib file并添加了Collection View以加载Collection View Cell

我想选择多个文件并通过delete button删除它们。

但是,当我按以下代码单击delete button时,无法获得该单元格。

if(self.collectionView.indexPathsForSelectedItems > 0){
NSLog(@"indexPathsForSelectedItems...count.%@..@@@" , self.collectionView.indexPathsForSelectedItems);


for(NSIndexPath *indexPath in self.collectionView.indexPathsForSelectedItems){

// [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", directory, [fileList objectAtIndex:indexPath.row]];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil] ;
AITFileCell_grid *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"AITFileCell_grid" forIndexPath:indexPath];
}
}

但是卖出似乎不正确...

如何获取selected cell 中的 collectionView

最佳答案

通常,您的删除按钮应具有如下选择器:

deleteButton = [[UIButton alloc] init];
...
[deleteButton addTarget:self selector:@sel(buttonPressed:) forControlEvent:UIControlEventTouchUpInside];

我相信您可以使用以下方法获取indexPath:
-(void)buttonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;

CGPoint rootPoint = [button convertPoint:CGPointZero toView:self.collectionView];

NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:rootPoint];

// -----------------------------------------------------------------
// Now you have your indexPath, you can delete your item
// -----------------------------------------------------------------



// first update your data source, in this case, I assume your data source
// is a list of values stored inside a NSArray

NSMutableArray *updatedList = [self.myListData mutableCopy];

[updatedList removeObjectAtIndex:indexPath.row];

self.myListData = updatedList;


// now update your interface

[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPath:@[indexPath]];
}];
}

关于ios - 如何在UICollectionView中获取选定的CollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24298609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com