1

典型的な状況を作りたいと思います: ユーザーがセルを選択すると、accessoryType がチェックマークになります。チェックマークを付けることができるのは、1 つのセルの accessoriesType だけです。そして、NSUserDefaults indexPath.row に保存して、アプリが選択したセル ユーザーを認識し、オプションを変更できるようにします。だから私はこの間違ったコードを書きました:

didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // checkedIndexPath is NSIndexPath

    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    self.checkedIndexPath = indexPath;

    [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInt:self.checkedIndexPath.row]forKey:@"indexpathrow" ];
} 

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Part of code from cellForRowAtIndexPath

    if(indexPath.row == [[[NSUserDefaults standardUserDefaults]objectForKey:@"indexpathrow"]intValue ])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

        return cell;
}

ただし、このコードはうまく機能しません。を開くとUITableView、テーブルには既に選択されているセルがあり、別のセルを押すと 2 つのcheckmarkedセルがあります...どうすればコードを改善できますか、それとも全体を変更する必要がありますか? 助言がありますか ?ありがとう !

4

1 に答える 1