如何在 UICollectionView 中获取选中的 CollectionViewCell? [英] How to get the selected CollectionViewCell in the UICollectionView?

查看:60
本文介绍了如何在 UICollectionView 中获取选中的 CollectionViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new for IOS development , and I try to use UIcollectionView to show the photo without using storyboard.

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

I create a xib file call AITFileCell_grid.xib , and add a Collection View Cell.

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

I also create another xib file and add the Collection View for loading Collection View Cell.

我想选择多个文件并通过删除按钮删除它们.

I want to selected multiple file and delete them by delete button.

但是当我通过以下代码单击删除按钮时无法获取单元格.

But I can not get the cell when I click the delete button by following code.

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];
        }
    }

但销售似乎不正确...

But the sell seems not correct...

如何在collectionView中获取selected cell?

推荐答案

通常你的删除按钮应该有一个这样的选择器:

Usually your delete button should have a selector like this:

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

我相信您可以使用以下方法获取 indexPath:

I believe you can get the indexPath using the following:

-(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]];
    }];
}

这篇关于如何在 UICollectionView 中获取选中的 CollectionViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆