我有一个 UITableView 显示内容列表,我希望列表中的每个项目都有一个复选框,可以通过用户触摸进行标记和取消标记。我为每个单元格创建了一个 UIButton,将其设置为单元格的附件视图,并添加了要调用的目标方法。
但是,每当我尝试单击复选框时,我总是会收到“无法识别的选择器发送到实例”错误,我不知道为什么。我到处查看以找出导致错误的原因,并确保我的 addTarget 调用和选择的方法使用正确的语法,但也许我遗漏了一些东西。我需要更改什么来修复此错误?
以下是创建单元格的代码:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// Set up the cell...
PFObject *tempMap = [searchResults objectAtIndex: [indexPath row]];
cell.textLabel.text = [tempMap objectForKey:@"mapName"];
// Add checkbox to cell
UIButton *checkBox = [UIButton buttonWithType:UIButtonTypeCustom];
checkBox.bounds = CGRectMake(0, 0, 30, 30);
cell.accessoryView = checkBox;
checkBox.tag = indexPath.row;
[checkBox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkBox setImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkBox setImage:[UIImage imageNamed:@"checkbox-pressed.png"] forState:UIControlStateHighlighted];
[checkBox addTarget:self action:@selector(checkBoxButton:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkBox];
}
return cell;
}
这是被调用的方法,checkBoxButton:
- (void)checkboxButton:(id)sender
{
UIButton *checkBox = sender;
if (checkBox.selected)
{
[selectedMaps removeObject:[searchResults objectAtIndex:checkBox.tag]];
NSLog(@"..Map Deselected..");
}
else
{
[selectedMaps addObject:[searchResults objectAtIndex:checkBox.tag]];
NSLog(@"..Map Selected..");
}
}