2012-03-06 98 views
3

我正在開發具有最新的SDK和的XCode 4.2的iOS 4的應用程序。用UIButton自定義UITableViewCell:哪個按鈕被點擊過?

我有一個UITableView用切片和定製UITableViewCell。每個單元格都有一個UIButton,並且所有這些按鈕具有相同的目標UIControlEventTouchUpInside

這是我的代碼:

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

    CalendarEventCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:nil options:nil]; 

     for(id currentObject in topLevelObjects) 
     { 
      if ([currentObject isKindOfClass:[CalendarEventCell class]]) 
      { 
       cell = (CalendarEventCell *)currentObject; 
       [cell.addToCalendarButton addTarget:self action:@selector(addEventToiCal) forControlEvents:UIControlEventTouchUpInside]; 
       break; 
      } 
     } 
    } 
... 
} 

當用戶觸摸該按鈕內,我怎麼能知道哪個部分和行是已單擊單元格?

+1

您可以能夠爲這個按鈕設置標籤。通過使用該標籤號碼,您可以找出哪個按鈕被點擊。 – Ganesh 2012-03-06 07:42:43

+0

看看這裏:http://stackoverflow.com/questions/510393/how-to-pass-a-variable-to-a-uibutton-action – pkyeck 2012-03-06 07:45:56

回答

6

將按鈕的標籤。例如:

CalendarEventCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell == nil) 
{ 
    NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:nil options:nil]; 

    for(id currentObject in topLevelObjects) 
    { 
     if ([currentObject isKindOfClass:[CalendarEventCell class]]) 
     { 
      cell = (CalendarEventCell *)currentObject; 
      [cell.addToCalendarButton addTarget:self action:@selector(addEventToiCal) forControlEvents:UIControlEventTouchUpInside]; 
      break; 
     } 
    } 
} 
cell.addToCalendarButton.tag = ((indexPath.section & 0xFFFF) << 16) | 
           (indexPath.row & 0xFFFF); 

您需要將您的選擇更改爲@selector(addEventToiCal:)和更新方法-(void) addEventToiCal:(UIButton *)sender

然後,您可以在單元格中,而不是目標設定爲控制器本身添加的東西像下面這樣-addEventToiCal:

if (!([sender isKindOfClass:[UIButton class]])) 
    return; 
NSUInteger section = ((sender.tag >> 16) & 0xFFFF); 
NSUInteger row  = (sender.tag & 0xFFFF); 
NSLog(@"Button in section %i on row %i was pressed.", section, row); 
+0

您還可以聲明方法' - (空)addEventToiCal: (UIButton *)sender'然後刪除第一行。 – 2012-03-06 07:48:46

+0

@MisterJack我辯論這樣寫。既然你似乎是第二個想法,我更新了答案,以反映你的建議。 – 2012-03-06 07:52:54

+0

感謝您的回答。我將需要indexPath.section和indexPath.row。 – VansFannel 2012-03-06 07:58:47

3

設置按鈕的目標的方法,創建一個委託協議與方法細胞如tappedButton:(UIButton *)button inCell:(UITableViewCell *)cell並將控制器設置爲單元的委託。在目標方法調用委託方法。

然後在控制器的委託方法實現中,您可以通過調用UITableViewtableView:indexPathForCell:找到單元的NSIndexPath

+0

將section和row傳遞給'tappedButton:'方法而不是調用'tableView:indexPathForCell:'可能會更容易一些,不是嗎? – VansFannel 2012-03-06 08:34:43

+0

然後你必須把cell的'NSIndexPath'作爲它的ivar/property - 這也是可能的。但是,您不必要地複製了您必須另外維護的數據 - 當單元格死亡時釋放它,並在例如更新時釋放它。上面的細胞被添加或移除等等。但是,如果通過運行'tableView:indexPathForCell:'遇到性能問題,這不太可能,那麼當然可以使用該「快捷方式」。 – macbirdie 2012-03-06 08:48:33

2

分配標籤值一樣,按鈕cellForRowAtIndexPath方法

  1. cell.addToCalendarButton.tag=indexPath.row
  2. 當您添加方法按鈕也送發件人所以分配方法,這樣的按鈕。

    [cell.addToCalendarButton addTarget:自動作:@selector(addEventToiCal:)forControlEvents:UIControlEventTouchUpInside];

  3. 在你的方法讀出的相關行像

    - (IBAction爲)addEventToiCal:(ID)發送方{ 的NSLog( 「當前行是%d」,[發送方標籤]); }

如果你想現在關於剖面然後indexPath做這種事再

- (void)addEventToiCal:(id)sender event:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition]; 

    NsLog("value of indePath.section %d ,indexPath.row %d",indexPath.section,indexPath.row); 

} 

在的cellForRowAtIndexPath就像那個指定你的方法。

[cell.addToCalendarButton addTarget:self action:@selector(addEventToiCal:event:)forControlEvents:UIControlEventTouchUpInside]; 
+0

感謝您的回答。我將需要indexPath.section和indexPath.row。 – VansFannel 2012-03-06 07:58:37

0

BNRXIBCell是iOS 5及以上版本的優秀解決方案。這是一個UITableViewCell子類,用於子類化,將操作消息從單元子視圖轉發給控制器。