0

因此,昨天运行良好的某些代码似乎无法正常运行。我在我的cellForRowAtIndexPath:方法中添加了一个选择器,但它总是抛出PAYPHProductTableViewCell valueChanged:]: unrecognized selector sent to instance 0x8d6a6c0. 这是我的代码:

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

这是选择器:

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

所有这些代码都在我的 ViewController 类中。我已经完成了其他答案,并使方法签名相同,但这无济于事。如果有人可以解释发生了什么,那就太好了,谢谢。

4

1 回答 1

1

异常消息几乎解释了一切。程序将消息发送到无法识别的对象。以下是一些检查代码的建议:

  1. 检查单元格的 .xib 文件并查看是否为相应对象正确设置了 UIStepper 类(如果它有出口)。
  2. 确保不要修改步进器属性。您可以通过覆盖属性的 setter 方法或简单地通过添加键值观察器来检查这一点。
  3. 就我个人而言,更好的方法是在单元格和 viewController 之间使用简单的委托。
于 2014-06-14T21:10:32.907 回答