2009-07-16 93 views
0

我是一個新手。自定義UITableViewCell項目問題

我有一個自定義的UITableViewCell有3項:

一個UILabel 爲UITextField 和一個UIButton

我的問題是,我怎麼知道從哪個單元中的按鈕被點擊或文本框進行編輯?

有沒有辦法跟蹤他們?

感謝您的回覆。

回答

1

我通常所做的是在配置自定義單元格後,我將單元格的NSIndexPath保存在狀態爲UIControlStateApplication的按鈕的標題中。

在你的tableView:的cellForRowAtIndexPath:方法,配置在小區:

[theButton setTitle:(NSString *)indexPath forState:UIControlStateApplication]; 

然後,在你的按鈕動作:


- (IBAction) buttonWasClicked:(UIButton *) senderButton { 
    NSIndexPath *indexPath; 

    indexPath = (NSIndexPath *)[senderButton titleForState:UIControlStateApplication]; 

    NSLog(@"Sender button was clicked in cell at NSIndexPath section: %d row: %d ", [indexPath section], [indexPath row]); 
} 

我不這樣真的很高興。如果有人有更乾淨的方式去做,我很樂意聽到它。

1

如果您只有一個部分,您可以將indexPath.row存儲在button.tag中,並在由該按鈕觸發的操作中檢索它。