0

So, some code that was working fine yesterday doesn't seem to be working correctly. I'm adding a selector in my cellForRowAtIndexPath: method, but it always throws PAYPHProductTableViewCell valueChanged:]: unrecognized selector sent to instance 0x8d6a6c0. Here's my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        //some code 
    }
    else
    {
        NSString *cellIdentifier = @"PAYPHProductTableViewCell";
        PAYPHProduct *product = self.myCart[indexPath.row - 1];
        PAYPHProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        [cell loadName:product.name Price:product.price Quantity:product.quantity];
        [cell.stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
        return cell;
    }
}

And here's the selector:

- (IBAction)valueChanged:(UIStepper *)sender
{
    int value = [sender value];
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    ((PAYPHProduct *) self.myCart[indexPath.row - 1]).quantity = value;
    [self updateNumItemsAndSubtotal];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath, [NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

All this code is within my ViewController class. I've gone through other answers and have made the method signatures identical, but that isn't helping. If someone could explain what's going on, that would be great, thanks.

Impossibility
  • 954
  • 7
  • 20
  • The error message is saying that you're calling valueChanged: on the cell instead of on the stepper. I don't see how that can be with the code you posted unless cell.stepper is retuning the cell instead of the stepper (which would be odd, but you should log cell.stepper to be sure what it's returning). You might also add an exception breakpoint to see what line is causing the error. – rdelmar Jun 14 '14 at 17:38

1 Answers1

1

The exception message pretty much explains everything. The program sends message to an object which doesn't recognize it. Here are some suggestions to check in your code:

  1. Check the .xib file of the cell and see if the UIStepper class is set correctly for the corresponding object (if it has an outlet).
  2. Make sure you don't modify the stepper property. You can check this by overriding the setter method of the property or simply by adding a key-value observer.
  3. To me personally, a better approach would be to use a simple delegation between the cells and the viewController.
e2l3n
  • 510
  • 1
  • 7
  • 15