0

我有个问题。

单击 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>>

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

谢谢!

4

3 回答 3

1

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

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

  }
于 2013-03-19T10:15:19.390 回答
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];
               }
    }
}
于 2013-03-19T11:49:21.860 回答
1

它在哪条线上给出了这个......

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

我认为在您的 cellForRowAtIndexPath 方法中,您还必须将按钮的标记值设置为 indexpath 行,即。

    cell.callButton.tag=[indexPath row]

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

& 仍然没有,那么您可以将按钮作为自定义视图添加到表格视图单元格,这样会更容易。

于 2013-03-19T10:28:15.143 回答