0

次のコードに問題があり、テーブルの行を 1 つだけ選択してチェックマークを表示する必要があります。現在、行に複数のチェックマークが表示されています。

助けてくれてありがとう

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Save the row selected to nsuserdefaults
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:cell.textLabel.text forKey:@"countrySaved"];

    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

        UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [checkmarkBtn setImage:image forState:UIControlStateNormal];
        [checkmarkBtn setFrame:CGRectMake(0, 0, 19, 19)];
        [checkmarkBtn setBackgroundColor:[UIColor clearColor]];
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

       UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       [checkmarkBtn setTitle:@"" forState:UIControlStateNormal];
       [checkmarkBtn setFrame:CGRectMake(0, 0, 0, 0)];
       [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

    }

}

以下を含める必要があるいくつかの投稿を見ましたが、コード内でそれを実装する方法がわかりません。

 cell.accessoryType = UITableViewAccessoryNone;

  if (indexPath.row == index) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
4

1 に答える 1

0

このコードで確認してください:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Save the row selected to nsuserdefaults

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:cell.textLabel.text forKey:@"countrySaved"];
    [prefs setObject:[NSNumber numberWithInt:indexPath.row] forKey:@"selectedRow"];
    [tableView reloadData];
}

cellForRowAtIndexPathの場合:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
int val = [[prefs objectForKey:@"selectedRow"] intValue];
if (indexPath.row == val)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

    UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [checkmarkBtn setImage:image forState:UIControlStateNormal];
    [checkmarkBtn setFrame:CGRectMake(0, 0, 19, 19)];
    [checkmarkBtn setBackgroundColor:[UIColor clearColor]];
    [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

} else
{
    cell.accessoryType = UITableViewCellAccessoryNone;

   UIButton *checkmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
   [checkmarkBtn setTitle:@"" forState:UIControlStateNormal];
   [checkmarkBtn setFrame:CGRectMake(0, 0, 0, 0)];
   [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = checkmarkBtn;

}
于 2013-01-28T14:15:04.987 に答える