【问题标题】:Buttons on a UICollectionViewCellUICollectionViewCell 上的按钮
【发布时间】:2012-09-26 19:21:14
【问题描述】:

新的 类之一是,它允许以类似于主屏幕/iBooks/Pages 布局的网格格式显示项目。

有没有办法在 UICollectionViewCell 上的按钮上接收触摸事件?目前,我的单元格是通过 XIB 文件创建的,并以编程方式添加到 UICollectionView。我正在寻找类似于 UITableView 上的 Detail Disclosure Indicator 的东西。

在此处查看 Apple 的文档后,我没有看到任何允许此类操作的方法,但我确信有办法做到这一点。

如何将按钮添加到 UICollectionViewCell 并在点击按钮时获取单元格的 indexPath?

是否有任何有用的教程或链接? iOS 6 相当新,所以我没有找到太多。提前致谢。

【问题讨论】:

    标签: ios6 uicollectionview objective-c ios6 uicollectionview


    【解决方案1】:

    一种方法是将NSIndexPath 属性添加到您的单元格,并在单元格出列时设置它。然后,您的单元格的操作处理程序将有权访问当前索引。

    UICollectionViewCell 的片段:

    @interface MyCustomCell : UICollectionViewCell
    @property (weak, nonatomic) NSIndexPath *indexPath;
    - (IBAction)testClicked:(id)sender; // TODO: wire up your button to this handler
    @end
    

    实现 UICollectionViewDataSource 的视图控制器的片段:

    -(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView
                     cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCustomCell *cell = 
            [collectionView dequeueReusableCellWithReuseIdentifier:@"myCustomCell"
                                                      forIndexPath:indexPath];
        cell.indexPath = indexPath;
        // TODO: other initialization for the cell
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      相关资源
      最近更新 更多