【问题标题】:How to get the selected CollectionViewCell in the UICollectionView?如何在 UICollectionView 中获取选中的 CollectionViewCell?
【发布时间】:2014-06-19 04:07:19
【问题描述】:

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

但卖出似乎不正确...

如何获取collectionView中的selected cell

【问题讨论】:

  • 先给delete.tag =indexpath.item这样的标签,然后在选择时维护数组。之后,当您想要删除而不是使用 for 循环时,您可以从数组中删除数据,只需 [collectionview reloaddata]

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    要检查单击了哪个集合单元,您可以在控制器中使用此委托方法。

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        int itemNo = (int)[indexPath row]+1;
        CollectionCell *selectedCell =(CollectionCell*)[collectionView cellForItemAtIndexPath:indexPath];
        switch (itemNo) {
            case 1:
            {
                //your Logic for 1st Cell
                break;
            }
            case 2:
            {
                //your Logic for 2nd Cell
                break;
            }
            .
            .
            .
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多