所以,一些昨天运行良好的代码似乎无法正常运行。我在我的 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 类中。我已经查看了其他答案并使方法签名相同,但这没有帮助。如果有人能解释发生了什么,那就太好了,谢谢。
最佳答案
异常消息几乎解释了一切。该程序向无法识别它的对象发送消息。以下是检查代码的一些建议:
- 检查单元格的 .xib 文件,看看是否为相应的对象正确设置了 UIStepper 类(如果它有 socket )。
- 确保您没有修改步进器属性。您可以通过覆盖属性的 setter 方法或简单地通过添加键值观察器来检查这一点。
- 就我个人而言,更好的方法是在单元格和 viewController 之间使用简单的委托(delegate)。
关于ios - 无法识别的选择器发送到实例(步进器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222393/