2014-03-03 36 views
-1

我想通過在iOS中使用uicollectionviewcell執行按鈕操作來獲取標籤值。我有四個標籤作爲子視圖添加到uicollectionviewcell中的uibutton。如何使用按鈕操作獲取所選索引uilabel值。如何在iOS中使用按鈕操作獲取標籤值

+0

'[UICollectionView indexPathsForSelectedItems]'?對我來說看起來很微不足道。 – trojanfoe

+0

請給我示例代碼,我是新的iPhone ... – user3331020

+2

不。你嘗試一個解決方案,並回發,如果你不能得到它的工作。 – trojanfoe

回答

0

您必須從UICollectionViewCell中創建一個自定義集合視圖單元類。在該類中,您可以從故事板創建標籤和按鈕。

#import <UIKit/UIKit.h> 

    @interface AudioListViewCell : UICollectionViewCell 

    @property (retain, nonatomic) IBOutlet UILabel *lbl1; 

    @property (retain, nonatomic) IBOutlet UILabel *lbl2; 

    @property (retain, nonatomic) IBOutlet UILabel *lbl3; 

    @property (retain, nonatomic) IBOutlet UIButton *button; 

    @end 

變化故事板作爲自定義單元格類name.Here AudioListViewCell的CollectionView細胞的類名。

在您的viewcontroller中的集合視圖的委託方法「didSelectItemAtIndexPath」中,您可以獲取標籤文本vlaue.This是一種方法。 如果要在按鈕單擊中獲取標籤文本,則必須在collectionvew單元格中爲按鈕添加選擇器,並且每個按鈕標記值與集合視圖索引相同。然後,您可以使用followng代碼獲取按鈕內的每個標籤。

 for (UILabel * lbl in button.subviews) { 

     if(lbl1.tag == uniqueLabelTag1)strVal1= lbl1.text; 

     else if(lbl2.tag == uniqueLabelTag2)strVal2= lbl2.text; 

     else if (lbl3.tag == uniqueLabelTag3) strVal3 = lbl3.text; 

    } 

您可以從按鈕標籤ie創建標籤標籤。例如,如果您的第一個收集視圖單元的按鈕標籤爲1,那麼您的lbl1標籤可以是1100,lbl2標籤可以是1200,而lbl3標籤可以是1300.如果第二個按鈕標籤是2,那麼lbl1標籤可以是2100,lbl2可以是2200和lbl3標籤可以2300等。