0

コンテンツのリストを表示する UITableView があり、リスト内の各項目に、ユーザーのタッチでマークおよびマーク解除できるチェックボックスが必要です。セルごとに UIButton を作成し、それをセルの accessoriesView に設定し、呼び出されるターゲット メソッドを追加しました。

ただし、チェックボックスをクリックしようとすると、常に「認識されないセレクターがインスタンスに送信されました」というエラーが表示され、その理由がわかりません。エラーの原因を突き止めるためにあらゆる場所を調べ、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..");
    }
}
4

1 に答える 1

2

セレクターを登録してcheckBoxButton:いますが、実際には実装していますcheckboxButton:(最初の「B」の大文字と小文字の違いに注意してください)。

于 2012-02-15T22:01:13.493 に答える