20

Ho una installazione UIViewController in uno storyboard con tableview e UISearchDisplayController.UISearchDisplayController e UITableView arresto anomalo prototipo cella

Sto cercando di utilizzare una cella prototipo personalizzata da self.tableview (che è connessa alla tabella principale nello storyboard). Funziona bene se self.tableview ha restituito almeno 1 cella quando carico la vista, ma se self.tableview non carica una cella (poiché non ci sono dati) e carico la UISearchBar e la ricerca, il metodo cellForRowAtIndexPath: si blocca:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomSearchCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomSearchCell" forIndexPath:indexPath]; 

    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

-(void)configureCell:(CustomSearchCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    User *user = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.nameLabel.text = user.username; 
} 

errori

*** Assertion failure in -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:] 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0x9ef3d00> {length = 2, path = 0 - 0}) 

mio fetchedResultsController sembra avere dati (1 sezione, 2 f) al punto suddetto metodo viene chiamato. Si blocca sulla linea dequeueReusableCellWithIdentifier.

Eventuali suggerimenti/idee? Dovrebbe deselezionare la cella prototipo da self.tableview, ma suppongo che non sia stato creato nessuno in self.tableview quindi questa è la causa?

+0

Puoi pubblicare il codice di 'configureCell: atIndexPath:' metodo?Il problema potrebbe essere lì –

+0

si blocca prima che arrivi ... Lo aggiungerò anche se – mootymoots

+0

aggiunto sopra ... non fa davvero molto – mootymoots

risposta

-1

Se si utilizza à UISearchDisplayController nel metodo celForRowAtIndexPath, è necessario utilizzare il parametro di parametro tableView e non il puntatore di mantenimento del controller.

provare con questo codice

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomSearchCell" forIndexPath:indexPath]; 
+3

Identificatore CustomSearchCell non esiste nella cella UISearchDisplayController. È una cella prototipo sulla tabella originale nello storyboard. Non è possibile aggiungere una cella prototipo a UISearchDisplayController nello storyboard ... AFAIK – mootymoots

7

Ho risolto questo duplicando il prototipo di cella in un nuovo XI ter:

In viewDidLoad:

[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"CustomSearchCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CustomSearchCell"]; 

E cellForRowAtIndexPath aggiornato per utilizzare Tableview del metodo non l'originale self.tableview:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomSearchCell" forIndexPath:indexPath]; 

    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 
60

UISearchDisplayController gestisce la propria UITableView (tabella filtrata), oltre ad avere la tabella principale. L'identificativo della cella nella tabella filtrata non corrisponde alla tabella principale. Anche voi volete prendere la cellula non da indexPath come entrambe le tabelle possono essere molto diversi gli uni dagli altri per quanto riguarda il numero di righe, ecc

Così, invece di fare questo:

UITableViewCell *cell = 
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier 
forIndexPath:indexPath]; 

invece fare questo:

UITableViewCell *cell = 
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
+3

Ho sofferto per ben 2 ore. La tua risposta ha aiutato molto, in particolare la tua nota che ometti perIndexPath perché le tabelle potrebbero avere indici molto diversi. Inoltre, per altri lettori: assicurati di avere self.tableview non solo tableview. – ThinkBonobo

+0

Fantastico. Ha reso la mia giornata. –

+1

Che Dio vi benedica – shannoga

0

questo è un argomento vecchio, ma se qualcuno corre lo stesso problema ancora, per me è stato correlato ad avere questa linea in viewDidLoad

self.tableView.estimatedRowHeight = 80; 

e allo stesso tempo l'attuazione del metodo delegato heightForRowAtIndexPath ad altezze variabili in differenti condizioni

if (indexPath.row == 0) { 
    return 44 + cellTopSpacing; 
} else { 
    return 44; 
} 

Rimozione l'altezza di righe stimato risolto.

Problemi correlati