1

私はxcodeを初めて使用します。このコードを使用して、テーブルの最大から10行を選択しています。このコードは機能していますが、そこから 1 つの行を選択すると、選択した他の値が自動的に選択されるという問題があります。これがどのようなエラーなのかわかりません。このエラーを削除するのを手伝ってください。ありがとうございました

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
    if(count < 10)
    {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [selectedobjects addObject:[NSNumber numberWithInt:indexPath.row]];
        count++;
    }

} else {
    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedobjects removeObject:[NSNumber numberWithInt:indexPath.row]];
    count --;
}
}
4

2 に答える 2

0

これはセルの再利用によるものです。cellForRowAtIndexPathで選択したセルを確認し、それに応じてアクセサリ タイプを指定する必要があります。選択した項目のindexPathindexPath.rowをasではなくdidSelectRowAtIndexPAth に保存することをお勧めしselectedObjectます。それからする

 - (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  // Do other stuff
  if ([selectedobjects containsObject:indexPath]) {
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
  }
  else
   {
     [cell setAccessoryType:UITableViewCellAccessoryNone];
   }
}
于 2013-06-08T09:40:35.743 に答える
0

ur .h dn' で 1 つの NSMutable Dictionary を取得し、プロパティを実行せず、Dictionary を合成します。

CellForRowAtIndex でこの行を使用します

if([tickmarkDictionary objectForKey:[NSString stringWithFormat:@"%d",[indexPath row]]])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

以下のように DidSelectRowAtIndex メソッドを使用します。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell= (UITableViewCell *) [tableView cellForRowAtIndexPath:indexPath];

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

        [tickmarkDictionary setValue:[NSString stringWithFormat:@"%@",thisCell.textLabel.text] forKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
    }
    else
    {
        if([tickmarkDictionary objectForKey:[NSString stringWithFormat:@"%d$%d",[indexPath section],[indexPath row]]])
        {
          [tickmarkDictionary removeObjectForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];

        }
        NSLog(@"dict count %d",[tickmarkDictionary count]);
        thisCell.accessoryType = UITableViewCellAccessoryNone;
    }

     NSLog(@"dict count %d",[tickmarkDictionary count]);
}

これで、テーブルを上下にスクロールし始めて、選択した行に戻ったときに、自動的に選択が解除されることはありません。

これを一度試してみてください。お役に立てば幸いです。

于 2013-06-08T11:18:06.797 に答える