2016-02-29 3 views
0

У меня есть контроллер с двумя массивами, один для обычных результатов; другой для фильтрованных/результатов поиска основан на вводе текста.При поиске, как сделать UITableViewCellAccessoryCheckmark правильной ячейкой?

Только одна ячейка может иметь UITableViewCellAccessoryCheckmark за раз.

Моя проблема может быть описана таким образом;

И.Е.:

  1. Посмотреть контроллер подается объект Venue; и помечен UITableViewCellAccessoryCheckmark. Это ожидается и правильно.
  2. Типы пользователей в поисковом запросе используется массив результатов поиска; не однако UITableViewCellAccessoryCheckmark больше не на Venue, который был ранее проверенного на шаге 1.

Я не знаю, почему его не проверяет ячейку.

Визуальные примеры;

Оригинальный формат. Cobo Arena это заранее выбранное место

First view

-

При вводе; Галочка не в нужном месте

Searching filters the results

-

Больше типирование: Галочка теперь ушел

More typing

-

код ниже

- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    FCVenue *venue; 

    if (self.isSearching) 
    { 
     venue = [self.searchResults objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     venue = [self.venues objectAtIndex:indexPath.row]; 
    } 

    if ([indexPath isEqual:self.selectedIndexPath]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%@", venue.name]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", venue.location]; 
} 

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.tableView cellForRowAtIndexPath:self.selectedIndexPath].accessoryType = UITableViewCellAccessoryNone; 
    self.selectedIndexPath = indexPath; 
    [self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    return indexPath; 
} 

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

    FCVenue *venue; 

    if (self.searching) 
    { 
     venue = [self.searchResults objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     venue = [self.venues objectAtIndex:indexPath.row]; 
    } 

    self.selectedVenue = venue; 

// This method fires a completion block and dismisses the view controller 
    if (self.completionBlock) 
    { 
     self.completionBlock(self, self.selectedVenue); 
    } 

    [self.navigationController dismissViewControllerAnimated:YES completion:^{ 
    }]; 
} 

ответ

1

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

Так что код должен быть что-то вроде этого, она не тестировалась, хотя:

- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    FCVenue *venue; 

    if (self.isSearching) 
    { 
     venue = [self.searchResults objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     venue = [self.venues objectAtIndex:indexPath.row]; 
    } 

    if ([venue isEqual:self.selectedVenue]) // You may want to compare just the id or any other unique property 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     // if you opt for keeping the selectedIndexPath property you need to refresh it here. 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%@", venue.name]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", venue.location]; 
} 

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //[self.tableView cellForRowAtIndexPath:self.selectedIndexPath].accessoryType = UITableViewCellAccessoryNone; 
    // Here you may want to do a loop over all the possible index path to clean the state or keep storing the selected indexPath just to clear the mark when the selected venue changes. 
    FCVenue *venue; 

    if (self.searching) 
    { 
     venue = [self.searchResults objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     venue = [self.venues objectAtIndex:indexPath.row]; 
    } 

    self.selectedVenue = venue; 
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    return indexPath; 
} 

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



// This method fires a completion block and dismisses the view controller 
    if (self.completionBlock) 
    { 
     self.completionBlock(self, self.selectedVenue); 
    } 

    [self.navigationController dismissViewControllerAnimated:YES completion:^{ 
    }]; 
} 

В любом случае, общая идея заключается в том, что вам нужно, чтобы связать место с чеком, а не на indexPath как indexPath изменится с поиском.

Смежные вопросы