2009-10-07 88 views
1

我正在使用自定義的UITableViewCell類。我的單元格上有多個按鈕(準確的說是4個),並且按鈕點擊在使用此單元類的UIViewController中處理。iPhone - 與自定義單元格dequeueReusableCellWithIdentifier問題

我正在嘗試使用按鈕的標記來計算單擊按鈕的行號。但是如果一個單元格沒有被創建而是使用一個空閒對象,那麼這樣做會導致一個問題。在這種情況下,標籤和行號不匹配。

有人可以告訴我如何處理這種情況?如果我將相同的標籤賦予不同行中的所有按鈕,如何識別按鈕被點擊的行?

非常感謝。

回答

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

    static NSString *CellIdentifier = @"Cell"; 

    MyTableCell *cell = (MyTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {  
     // whatever you have now 
    } 
    // Set up the cell... 
    cell.myListViewController = self; 
    int tag = indexPath.row; 
    cell.button1.tag = tag; 
    cell.button2.tag = tag; 
    cell.button3.tag = tag; 
.... 
} 

此代碼在每一行中都有一個唯一的按鈕標籤。您將標記設置爲不在新單元格創建中,但是包括重用在內的所有情況。

+0

所以你說我保留UITableViewCell類中的UIButton對象的實例? – lostInTransit 2009-10-07 10:25:54

+0

請注意,如果您有一個表格部分,則將該行用作標記只能可靠地工作。 – 2009-10-07 10:42:14

+0

是的,我只有一個部分。謝謝。 – lostInTransit 2009-10-07 11:16:11

相關問題