2013-07-05 24 views
0

的字符串屬性值。我有一個UITableView,它包含名爲「MyCustomCell」的原型單元。該「MyCustomCell」包含一個按鈕,名爲「cellKey」一個的NSString屬性:在ios應用程序中獲取自定義UITableViewCell

@interface MyCustomCell : UITableViewCell 
    @property (weak, nonatomic) IBOutlet UIButton *myButton; 
    @property (strong, nonatomic) NSString *cellKey; 
@end 
中的cellForRowAtIndexPath委託方法

,我分配cellKey並添加自來水監聽器按鈕。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 

    MyItem *item = [self.data objectAtIndex:row]; 

    NSString *identifier = @"MyCustomCell"; 
    MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier]; 

    //Give the cell the key 
    cell.cellKey = item.key; 
    //add a tap listener 
    [cell.myButton addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside]; 
    return cell; 
} 

在buttonTaped處理我想對應的按鈕,電池的關鍵點:

- (IBAction)buttonTaped:(id)sender 
{ 
    //Get the button 
    UIButton *senderButton = (UIButton *)sender; 
    //get the super view which is the cell 
    MyCustomCell *cell = (MyCustomCell *)[senderButton superview]; 
    //get the key 
    NSString *key = cell.cellKey; 

} 

然而,當我運行應用程序,並單擊該按鈕,應用程序崩潰時,我打電話cell.cellKey出現此錯誤:

-[UITableViewCellContentView cellKey]: unrecognized selector sent to instance 0x13576240 

它不認識到superView是MyCustomCell類型。那麼如何獲得包含點擊按鈕的單元格的「cellKey」屬性?

感謝

回答

1

你的按鈕其實就是加入細胞contentView所以你需要瀏覽一個多級了superview層次去細胞。

+0

謝謝,這是問題 – Youssef

相關問題