2016-02-19 44 views
1

我試圖設置爲tag按鈕內部電池:如何在單元格iOS中爲按鈕設置標籤?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    [cell.btnWithIdCell setTag: (int) self.responseObject[@"results"][indexPath.row][@"id"]]; 
} 

後在事件點擊按紐我試圖讓tag

- (IBAction)open:(id)sender { 
    NSString* tagString = [NSString stringWithFormat:@"%d", [sender tag]]; 
} 

它給了我錯號碼tagString

如何從響應對象設置按鈕標籤?按鈕在細胞

+0

您可以顯示整個'cellForRowAtIndexPath'方法,並嘗試在此方法和'open'中記錄您的標記值。 –

+0

我認爲你有IBAction按鈕的問題。現在在現代ObjectiveC中,您必須使用Associated方法。對於objC來說非常簡單快捷。檢查我的答案。 – Vvk

回答

1

組標籤:

cell.btnWithIdCell.tag = indexPath.row; 
+0

我需要只從響應對象中設置標記,而不是索引 – DjamilBahirov

0

只要使用響應對象的(應該是在可以分配給標籤類型)代替indexPath的,並根據應用程序的要求在按鈕操作使用邏輯。

-1
在IBAction爲

請使用- (IBAction)open:(UIButton *)sender insted的的- (IBAction)open:(id)sender 如果沒有工作,然後用另一種方式來獲得數據IBAction爲類似下面的方法非常簡單,非常快insted的的tag

#import <objc/runtime.h> 

然後使用你tableview方法和IBAction同樣喜歡下面的代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    objc_setAssociatedObject(cell.btnWithIdCell, @"yourObjKey", self.responseObject[@"results"][indexPath.row][@"id"], OBJC_ASSOCIATION_RETAIN_NONATOMIC); 

    [cell.btnWithIdCell addTarget:self action:@selector(open:) forControlEvents:UIControlEventTouchUpInside]; 
} 

- (IBAction)open:(UIButton *)sender { 

    NSString *tagString = objc_getAssociatedObject(sender, @"yourObjKey"); 
} 
0

什麼是「錯」的標籤?因爲標籤是一個NSInteger,我相信你的電話(int) self...[indexPath.row][@"id"]返回一個參考對象,這是不是一個NSInteger的,但也許是的NSNumber *

所以你需要添加[... integerValue]到通話結束設置按鈕的標籤時,

相關問題