2015-02-10 72 views
1

我有一個UICollectionView它有2個部分。每個部分標題都有一個按鈕。我試圖將部分ID發送到prepareForSegue上的另一個視圖控制器,如下所示。如何識別哪個按鈕點擊哪個部分UICollectionView iOS?

if ([[segue identifier] isEqualToString:@"ManualCapture"]){ 
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender]; 

    ManualCapture *vc = [segue destinationViewController]; 
    vc.sectionId = indexPath.section; 
} 

但它發送0作爲部分ID。有沒有任何可能的方式來獲得部分ID?如何根據按鈕點擊發送sectionId。基本上我想這樣做,當我點擊第1部分中的按鈕,在prepareForSegue上它應該被髮送0,如果我點擊了第2節中的按鈕,在prepareForSegue上它應該被髮送1.我該怎麼做?

在此先感謝!在集合視圖的委託方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
NSString *tagStr = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row]; 
yourButton.tag = [tagStr intValue]; 
} 

然後當你要檢查哪個部分或指數是獲得它通過它的標籤,就像這樣:

+0

'sender'確實是單元格還是實際上是按鈕?它需要是'indexPathForCell:'的單元格才能工作。 – rmaddy 2015-02-10 05:25:51

+0

實際上是一個按鈕。那我該怎麼做? – codebot 2015-02-10 05:27:58

+0

如果你將一個非正確的單元格傳遞給'indexPathForCell:',那麼得到的'indexPath'將會是'nil',這就是爲什麼你得到0的部分。 – rmaddy 2015-02-10 05:29:28

回答

3

你可以像下面這樣做

UIButton *temp = (UIButton *)sender; 
NSString *tempStr = [NSString stringWithFormat:@"%d",temp.tag]; 

然後比較它。