0

ユーザーが新しいセルを選択するときに、選択したセルの選択を解除する必要があるアプリケーションがあります。テーブルビューの行の選択と選択解除を行うことはできますが、問題は、一度に1つの行のみを選択する必要があることです。私は今こうやっていた、

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];   

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

     cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; 

}

しかし、これでは同じ行を選択および選択解除できます。しかし、私の意図は、他のセルがすでに選択されている場合に新しい行を選択するときに、選択を解除する必要があることです。どんな体も私を助けることができますか?

4

6 に答える 6

6

素朴な解決策は

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
      [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
  } 
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];   

}

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

     cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; 

}

基本的に、新しいセルを選択するたびに、すべてのセルをループして選択を解除します。

于 2013-01-26T06:53:39.007 に答える
1

使用する :

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
于 2013-01-26T06:36:37.610 に答える
0

あなたが使用することができますdeselectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
          [tableView deselectRowAtIndexPath:indexPath animated:YES];

    }
于 2013-01-26T06:39:31.457 に答える
0

以下のコードを試してみてください。

int rowNumber = [tableCategory numberOfRowsInSection:0];

for (int indexRow = 0 ; indexRow < rowNumber; indexRow++) {

[NSIndexPath indexPathForRow:indexRow inSection:0];

[tableCategory cellForRowAtIndexPath:indexPath] ;

<required indexpath row>)

cell

cell

}
于 2013-01-26T06:48:31.177 に答える
0

以下を試してください:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
    [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]]; 
  }

   cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];   
}
于 2013-01-26T07:29:30.240 に答える