0

У меня есть табличный вид с контроллером отображения поиска. Когда я печатаю поиск, журнал показывает, что поиск работает и правильно фильтруется, однако на экране отображается только «Нет результатов». Я использую Core Data, если это имеет значение.Результаты поиска не отображаются

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
if (tableView == self.searchDisplayController.searchResultsTableView) { 
    return [searchResults count]; 
} 
else{ 
    return[[self.fetchedResultsController sections]count]; 
} 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\ 
return [secInfo numberOfObjects]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell; 
static NSString *CellIdentifier = @"Cell"; 

if (tableView != self.tableView) { 
    NSLog(@"Found searchDisplayController"); 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} else { 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    NSLog(@"Found regular table View"); 
} 

    // Configure the cell... 
    Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath]; 


    if (tableView == self.searchDisplayController.searchResultsTableView) { 
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; 
    } else { 
    cell.textLabel.text = instance.name; 
    } 

Вот NumberOfRowsInSection и номер:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
    return [searchResults count]; 
} 
    else{ 
    return[[self.fetchedResultsController sections]count]; 
} 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\ 
    return [secInfo numberOfObjects]; 
} 
+0

где вы обновляете содержимое ячейки? – Max

+0

Ниже: Экземпляр * instance = [self.fetchedResultsController objectAtIndexPath: indexPath]; // больше данных поиска if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [searchResults objectAtIndex: indexPath.row]; } else { cell.textLabel.text = имя экземпляра; } – Isaac

+0

@ Исаак: Можете ли вы добавить этот код к своему вопросу? Это облегчает чтение. - Методы 'numberOfSections' и' numberOfRowsInSection' также будут полезны. –

ответ

0

Проверьте вашу реализацию numberOfSectionsInTableView и numberOfRowsInSection.
В первом случае вы должны вернуть 1, если это вид таблицы поиска (1 раздел).
В numberOfRowsInSection вы также должны проверить заданный аргумент tableView, как и в numberOfSectionsInTableView, и вернуть [searchResults count].

0

не должен ваша реализации быть что-то вроде: -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     return 1; 
    } 
    else 
    { 
     return[[self.fetchedResultsController sections]count]; 
    } 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     return [searchResults count]; 
    } 
    id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\ 
    return [secInfo numberOfObjects]; 
} 

Если количество результатов поиска высоко, то я предложил бы использовать другие извлечённые Результаты контроллера вместо массива.

+0

спасибо за это. Я внедрил его и ввел записи журналов, чтобы узнать, как работает код. Все было видно, и в журнале я вижу, что поиск точно фильтрует мой поиск, но на дисплее по-прежнему отображается «Нет результатов». Любые другие места, которые я должен посмотреть? – Isaac

+0

Если вы видите физическое сообщение «Нет результатов», тогда есть ли какой-либо код, который выводит это сообщение? – Max

+0

Я так не думаю. В CellForRowAtIndexPath я определил, что должно заполнить ячейку, если tableView == self.searchResultsController.searchResultsTableView. Не следует ли настраивать ячейки результатов поиска? – Isaac

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