Pergunta

I have a problem.

I am trying to pass parameter after clicking on button that is subview of UITableViewCell, Here is my source code:

    - (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;
}

and:

- (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];
    }
 }

everything seems to be OK BUT there is a problem when I debug I get:

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

How can I recognize the button as button and not as a cell?

Thanks!

Foi útil?

Solução

Try this inside

   - (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];
               }
    }
}

Outras dicas

If the sender is a MSContactCell why not use it to get the contactButton ?

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

  }

On which line it gives this...

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

I think in your cellForRowAtIndexPath method you also have to set the tag value of button as indexpath row i.e .

    cell.callButton.tag=[indexPath row]

so you can easily recognize each button.

& still not then you can add button as custom view to table view cell then it will be more easy.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top