2012-09-26 29 views
1

其中一個新的類是,它允許以類似於Homescreen/iBooks/Pages佈局的網格格式顯示項目。UICollectionViewCell上的按鈕

有沒有辦法在UICollectionViewCell上的按鈕上接收觸摸事件?目前,我的單元格是通過XIB文件創建的,並以編程方式添加到UICollectionView。我正在尋找類似於UITableView上的Detail Diclosure Indicator的東西。

看過Apple的文檔後,我沒有看到任何方法允許這樣的事情,但我肯定有一種方法可以做到這一點。

如何在按鈕被點擊時向UICollectionViewCell添加按鈕並獲取單元格的indexPath?

是否有任何教程或鏈接可能有幫助嗎? iOS 6是相當新的,所以我沒有找到太多東西。提前致謝。

回答

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