IPhone: выбран другой ряд - PullRequest
0 голосов
/ 12 января 2012

Когда я выбираю строку из 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];
}
}

Заранее спасибо!

Ответы [ 2 ]

2 голосов
/ 12 января 2012

Это, вероятно, потому что клетки используются повторно. Если вы хотите использовать цвет фона для отображения выбранного состояния, вам нужно установить его в методе создания ячейки

Добавление этого кода должно работать:

- (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];
    }

}
0 голосов
/ 12 января 2012

Да, вы должны объявить новый NSMutableArray (скажем, _selectedList) вашего источника данных. Заполните его NSNumber значением 0.

Объявить 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]];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...