0

UITableViewから行を選択すると、その行とその下の行(選択した行の下のいくつかの行)も選択されます。選択された行のみが選択された行であることが期待されます。

私のコードは次のとおりです。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    //Deselect
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.backgroundColor=[UIColor clearColor];
} else {
    //Select
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.backgroundColor=[UIColor redColor];
}
}

前もって感謝します!

4

2 に答える 2

2

これはおそらく、セルが再利用されているためです。背景色を使用して選択した状態を表示する場合は、セルゲッターメソッドで設定する必要があります

このコードを追加すると機能するはずです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //...
    if (!cell.selected) {
        //Deselected
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor=[UIColor clearColor];
    } else {
        //Selected
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.backgroundColor=[UIColor redColor];
    }

}
于 2012-01-12T10:13:31.173 に答える
0

はい、データソース数の新しいNSMutableArray(たとえば)を宣言する必要があります。_selectedList値0のNSNumberを入力します。

NSMutableArray *_selectedList; .hファイルで宣言する(クラスメンバーとして)

viewDidLoadまたはinitメソッドで、

_selectedList = [[NSMutableArray alloc] init];
for( int i = 0; i < [datasource count]; i++ )
{
  [_selectedList addObject:[NSNumber numberWithBool:NO]];
}

そして、次のように次のようにします。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //...
    if (! [[_selectedList objectAtIndex:indexPath.row] boolValue]) {
        //Deselected
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor=[UIColor clearColor];
    } else {
        //Selected
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.backgroundColor=[UIColor redColor];
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    //Deselect
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.backgroundColor=[UIColor clearColor];
  } else {
    //Select
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.backgroundColor=[UIColor redColor];
  }
  BOOL isSelected = ![[_selectedList objectAtIndex:indexPath.row] boolValue];
  [_selectedList replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isSelected]];
}
于 2012-01-12T10:58:49.033 に答える