views:

150

answers:

1

I currently have a button defined in a cell and a method to track its UITouchDown action as shown:

- (void) clickedCallSign:(id)sender {

    int index = [sender tag];
    NSLog(@"event triggered %@",index);

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    //Callsign button
    UIButton *button;

    CGRect rect = CGRectMake(TEXT_OFFSET_X, BORDER_WIDTH, LABEL_WIDTH, LABEL_HEIGHT);
    button = [[UIButton alloc] initWithFrame:rect];
    cell.tag=[indexPath row];
    button.tag=[indexPath row];
    [button addTarget:self action:@selector(clickedCallSign:) forControlEvents:UIControlEventTouchDown];
    [button setBackgroundColor:[UIColor redColor]];
    [button setTitle:@"hello" forState:UIControlStateNormal];
    [cell.contentView addSubview:button];
    [button release];   
}

However when I click a cell in the simulator, the console debug message is: "event triggered (null)" and my app crashes shortly after.

How can I correctly get the indexPath.row value into my clickedCallSign method?

+1  A: 

First off, index is an int, so your NSLog needs to look like this (note the %d):

NSLog(@"event triggered %d", index);

(It's possible that this leads to a crash, but it's also likely that something else entirely is going that causes the instability.)

quixoto
@quixoto: the '%d' did the trick of getting the indexPath.row value into the clickedCallSign :) the app still crashes though.. with this stack trace:-[UIButton setText:]: unrecognized selector sent to instance 0x6877a102010-08-13 21:48:30.324 RF[8047:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]: unrecognized selector sent to instance 0x6877a10'
dpigera
You're probably over-releasing something somewhere. The memory management in the code snippet you show looks fine, so something elsewhere is wrong.
quixoto
actually.. found the solution i needed right here:http://developer.apple.com/iphone/library/samplecode/Accessory/Introduction/Intro.htmlThanks for the help though. I really appreciate it.
dpigera