2011-07-12 67 views
0

我用customcell做了一個tableview。單元格包括標籤和一個按鈕。問題是,如何在按下按鈕時檢測標籤的文本?Tableview自定義單元格按鈕

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { 
    // Initialization code 
     primaryLabel = [[UILabel alloc]init]; 
     primaryLabel.textAlignment = UITextAlignmentLeft; 
     primaryLabel.font = [UIFont systemFontOfSize:11]; 
     primaryLabel.backgroundColor = [UIColor clearColor]; 
     secondaryLabel = [[UILabel alloc]init]; 
     secondaryLabel.textAlignment = UITextAlignmentLeft; 
     secondaryLabel.font = [UIFont systemFontOfSize:9]; 
     secondaryLabel.backgroundColor = [UIColor clearColor]; 
     myImageView = [[UIImageView alloc]init]; 

     btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn.backgroundColor = [UIColor clearColor]; 

     [self.contentView addSubview:btn]; 
     [self.contentView addSubview:primaryLabel]; 
     [self.contentView addSubview:secondaryLabel]; 
     [self.contentView addSubview:myImageView]; 
    } 

    return self; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    return cell; 
} 
+0

這並不難,但這取決於你如何設置單元格。您可以向我們展示您創建單元格,標籤和按鈕的代碼嗎? – sosborn

+0

我也有一個提醒功能,我必須捕捉單元格內容並設置日曆的事件控制器。 – Ulvi

回答

0

你正在尋找的方法是 - addTarget:動作:forControlEvents:

使用你比如你有一個UIButton,BTN,讓我們說,你有你叫myAction自定義單元格的動作。代碼如下所示:

[btn addTarget:self action:myAction forControlEvents:UIControlEventTouchUpInside]; 

因爲myAction是您的單元格中的方法,所以您可以輕鬆訪問標籤屬性。如果你需要把不同的控制你的行動:

[btn addTarget:otherController action:myAction forControlEvents:UIControlEventTouchUpInside]; 

在這種情況下,你將有一個參考保持在控制你的電池。既然你可能有多個單元格,你可以保留一個NSMutableArray作爲屬性,並在構建表格時添加單元格。

+0

感謝您的回答。我會嘗試。 – Ulvi

相關問題