其中一个新的ios6类是uicollectionview,它允许以类似于Homescreen / iBooks / Pages布局的网格格式显示项目。
是否有办法在UICollectionViewCell上的按钮上接收触摸事件?目前,我的单元格是通过XIB文件创建的,并以编程方式添加到UICollectionView中。我正在寻找类似于UITableView上的Detail Diclosure指标的东西。
在查看Apple的文档后,我没有看到任何允许类似内容的方法,但我很肯定有一种方法可以做到。
如何在点击按钮时向UICollectionViewCell添加按钮并获取单元格的indexPath?
是否有任何可能有用的教程或链接? iOS 6相当新,所以我找不到多少。提前谢谢。
答案 0 :(得分:2)
一种方法是向您的单元格添加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;
}