2012-03-06 97 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子类,用于子类化,将操作消息从单元子视图转发给控制器。