在按钮操作上传递参数:通过点击UIButton从UITableviewCell传递@selector

时间:2013-03-19 10:05:56

标签: iphone uitableview selector sender

我有一个问题。

我试图在点击UITableViewCell的子视图按钮后传递参数,这是我的源代码:

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

    MSContactCell *cell = (MSContactCell *)[_tableView dequeueReusableCellWithIdentifier: cellWithCheckId];
    MSContact *contact = [self.list objectAtIndex:indexPath.row];

    if (cell == nil)
        {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MSContactCell"
                                                  owner:self
                                                options:nil] lastObject];

          cell.delegate = self;
        // cell.index = indexPath.row;

            cell.backgroundView.backgroundColor = [UIColor colorWithRed:0.882 green:0.863 blue:0.839 alpha:1.0];
        }
    cell.title.text = [contact contactName];
    cell.jobTitle.text = [contact jobDescription];

    cell.callButton.contactStr = [contact phoneNumber];
      [cell.callButton addTarget:self action:@selector(callPressed:) forControlEvents:UIControlEventTouchUpInside];         
    return cell;
}

- (IBAction)callPressed:(id)sender
{
        contactButton *button = (contactButton *)sender;

    UIDevice *device = [UIDevice currentDevice];
    if ([[device model] isEqualToString:@"iPhone"] ) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", button.contactStr]]];
    } else {
        UIAlertView *Notpermitted=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [Notpermitted show];
        [Notpermitted release];
    }
 }

一切似乎都没问题但是当我调试时出现问题:

 (lldb) po button
   $4 = 0x2084d5a0 <MSContactCell: 0x2084d5a0; baseClass = UITableViewCell; frame = (0 0; 320 95);        
   autoresize = W; layer = <CALayer: 0x2085dd70>>

如何将按钮识别为按钮而不是单元格?

谢谢!

3 个答案:

答案 0 :(得分:1)

如果发件人是MSContactCell,为什么不使用它来获取contactButton

 - (IBAction)callPressed:(id)sender
  {
    contactButton *myCell = (MSContactCell *)sender;
    contactButton *button = myCell. callButton;
    ...

  }

答案 1 :(得分:1)

在哪一行上给出了这个......

      (lldb) po button
      $4 = 0x2084d5a0 <MSContactCell: 0x2084d5a0; baseClass = UITableViewCell; frame  = (0 0; 320 95);        
    autoresize = W; layer = <CALayer: 0x2085dd70>>

我认为在你的cellForRowAtIndexPath方法中你还必须将按钮的标记值设置为索引路径行,即

    cell.callButton.tag=[indexPath row]

这样您就可以轻松识别每个按钮。

&安培;仍然没有那么你可以添加按钮作为自定义视图到表视图单元格然后它将更容易。

答案 2 :(得分:1)

中试试
   - (IBAction)callPressed:(id)sender
{
    for (UIView *parent = [sender superview]; parent != nil; parent = [parent superview]) {
            if ([parent isKindOfClass: [UITableViewCell class]]) {
                UITableViewCell *cell = (UITableViewCell *) parent;
                UIButton * button =  (UIButton *)[cell viewWithTag:yourButtonsTag];
               }
    }
}
相关问题