views:

28

answers:

2

I built the cell with Interface Builder. I load the cells like this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"MassCircleNGTableCell" owner: self options: nil];
    cell = circleNGCell;
    self.circleNGCell = nil;
}   
UISwitch *s = (UISwitch*)[cell.contentView viewWithTag: 20];
UILabel  *label = (UILabel*)[cell.contentView viewWithTag:19];
label.text = @"some useful text";

And that part works, I get my table with custom cells. But, although I can set the initial state, I don't know how to respond to the user flipping the switch! I tried this:

[leftSwitch addTarget:self action:@selector(setCircle) forControlEvents:UIControlEventTouchUpInside];

But the app crashes with "[MassCircleNGViewController setCircle]: unrecognized selector sent to instance 0xdd024b0'"

Any ideas out there?

[Added Later (as requested)]:

// implementation of [MassCircleNGViewController setCircle] really just a stub here
- (IBAction) setCircle: (id) sender {
    NSLog(@"setCircle sender == %@", (UISwitch*)sender);
}

The real problem was how I was trying to add the target. Need that colon after the selector name in the argument list!

[leftSwitch addTarget:self action:@selector(setCircle:) forControlEvents:UIControlEventValueChanged];
A: 

There may be 1 of these problems:

1/ Do you have a method called setCircle inside your UITableViewCell, MassCircleNGViewController

2/ Double check all the release. Maybe your UITableViewCell is released when the method is invoked and another object in the same memory area now

vodkhang
The setCircle method is declared/defined in the MassCircleNGViewController class. I am not calling retain or release anywhere. From the console log, I would think this is not a non-existent object problem. I did copy the addTarget call from some code I found here on StackOverflow. Maybe I am doing that wrong?
Paul
can you post the code of the setCircle by editting your question? I want to see the method
vodkhang
+1  A: 

Okay, I found the problem. Where I had done this:

 [leftSwitch addTarget:self action:@selector(setCircle) forControlEvents:UIControlEventTouchUpInside];

I needed to add a COLON after the selector name to get this:

 [leftSwitch addTarget:self action:@selector(setCircle:) forControlEvents:UIControlEventTouchUpInside];

I know I saw a comment about doing that somewhere in these fora, but it took a couple of hours to sink in, and I have forgotten where I saw it. Oh, I did find a Very Nice Tutorial on XIB based custom UITableViewCells here

Paul