Analisi della ricerca UITableview: nessun risultato
-
20-12-2019 - |
Domanda
Sto cercando di usare a UISearch
barra per poter trovare tutti gli oggetti di una persona specifica.L'ho impostato in modo che ciascuno PFUser
o l'utente ha un oggetto nome all'interno dell'oggetto principale di points
.Quando provo a trovare l'utente e gli oggetti non appare, ma negli NSLog ottengo tutte le informazioni.Penso che il mio problema sia nel cellForRowAtIndexPath
, Ma non so perché!
Per prima cosa chiedo:
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:@"points"];
// The key of the PFObject to display in the label of the default cell style
//self.textKey = @"opponent";
// Uncomment the following line to specify the key of a PFFile on the PFObject to display in the imageView of the default cell style
// self.imageKey = @"image";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
// The number of objects to show per page
self.objectsPerPage = 50;
//where you set the username to the specific user to get only this user's post.
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[query orderByDescending:@"createdAt"];
return query;
}
Metto la mia barra:
-(void)viewDidAppear:(BOOL)animated{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
//CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
//self.tableView.contentOffset = offset;
self.searchResults = [NSMutableArray array];
}
Filtraggio dei risultati:
-(void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName: @"points"];
[query whereKeyExists:@"name"]; //this is based on whatever query you are trying to accomplish
[query whereKeyExists:@"opponent"]; //this is based on whatever query you are trying to accomplish
[query whereKey:@"name" containsString:searchTerm];
NSArray *results = [query findObjects];
NSLog(@"%@", results);
NSLog(@"%u", results.count);
[self.searchResults addObjectsFromArray:results];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
Counting The Objects:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
//if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.objects.count;
} else {
return self.searchResults.count;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110;
}
Cercando di mostrare tutto!:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
// If you want a custom cell, create a new subclass of PFTableViewCell, set the cell identifier in IB, then change this string to match
// You can access any IBOutlets declared in the .h file from here and set the values accordingly
// "Cell" is the default cell identifier in a new Master-Detail Project
static NSString *CellIdentifier = @"celltag";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (tableView != self.searchDisplayController.searchResultsTableView) {
UILabel *game = (UILabel*) [cell viewWithTag:200];
game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];
UILabel *points = (UILabel*) [cell viewWithTag:201];
points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"],[object objectForKey:@"ppg"]];
UILabel *date = (UILabel*) [cell viewWithTag:250];
date.text = [object objectForKey:@"date"];
UILabel *name = (UILabel*) [cell viewWithTag:203];
name.text = [object objectForKey:@"name"];
}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
UILabel *game = (UILabel*) [cell viewWithTag:200];
game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];
UILabel *points = (UILabel*) [cell viewWithTag:201];
points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"],[object objectForKey:@"ppg"]];
UILabel *date = (UILabel*) [cell viewWithTag:250];
date.text = [object objectForKey:@"date"];
UILabel *name = (UILabel*) [cell viewWithTag:203];
name.text = [object objectForKey:@"name"];
}
UILabel *game = (UILabel*) [cell viewWithTag:200];
game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];
UILabel *points = (UILabel*) [cell viewWithTag:201];
points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"],[object objectForKey:@"ppg"]];
UILabel *date = (UILabel*) [cell viewWithTag:250];
date.text = [object objectForKey:@"date"];
UILabel *name = (UILabel*) [cell viewWithTag:203];
name.text = [object objectForKey:@"name"];
return cell;
}
Soluzione
Ci sono anche MOLTI errori nel tuo file cellForRowAtIndexPath
metodo.
(1) Contiene uno strano condizionale.
UN. Per prima cosa stai confrontando i puntatori con il file !=
operatore:
if (tableView != self.searchDisplayController.searchResultsTableView)
Ma poi stai confrontando gli oggetti usando isEqual
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
Sebbene entrambi i metodi siano sufficienti e questo condizionale dovrebbe tecnicamente funzionare, è meglio essere coerenti;e da tableView in cellForRowAtIndexPath
contiene un riferimento a un puntatore, l'uso degli operatori == o != non solo è sufficiente, ma è più veloce.
B. Anche usare due se in questo modo non è una buona forma dato che stai avendo if
dichiarazioni per entrambe le opzioni.Invece consiglio di usare un'istruzione if-else:
if (tableView != self.searchDisplayController.searchResultsTableView) {
...
} else {
...
}
return cell;
C. Il tuo condizionale, così com'è, è completamente inutile.Hai lo stesso codice in entrambi i blocchi condizionali e poi di nuovo anche DOPO il blocco condizionale!Quindi, così com'è, puoi rimuovere entrambe le istruzioni if poiché l'ultimo codice al di fuori delle istruzioni if è l'unico codice realmente eseguito.
(2) Ma tu Fare in effetti serve un condizionale anche se quello attuale è ridondante.
Il problema più importante sul motivo per cui la tabella non cambierebbe durante l'utilizzo del codice che hai specificato:
Dal momento che stai usando Parse's cellForRowAtIndexPath
metodo, i dati della riga vengono popolati con le informazioni da - (PFQuery *)queryForTable
, così come il tuo codice è attualmente scritto, gli oggetti verranno popolati con i risultati di questa query:
PFQuery *query = [PFQuery queryWithClassName:@"points"];
Puoi utilizzare il parametro Parse PFObject restituito da questa query quando non stai eseguendo la ricerca, ma utilizza l'oggetto nell'indice corrente dell'array self.searchResults, ad es. [self.searchResults objectAtIndex:indexPath.row]
, per quando stai cercando, ad esempio:
if (tableView != self.searchDisplayController.searchResultsTableView) {
UILabel *game = (UILabel*) [cell viewWithTag:200];
game.text = [NSString stringWithFormat:@"V.s %@", [object objectForKey:@"opponent"]];
UILabel *points = (UILabel*) [cell viewWithTag:201];
points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[object objectForKey:@"goals"], [object objectForKey:@"assists"], [object objectForKey:@"totalpoints"], [object objectForKey:@"plusminus"], [object objectForKey:@"hits"], [object objectForKey:@"ppg"]];
UILabel *date = (UILabel*) [cell viewWithTag:250];
date.text = [object objectForKey:@"date"];
UILabel *name = (UILabel*) [cell viewWithTag:203];
name.text = [object objectForKey:@"name"];
} else {
PFObject *searchResult = [self.searchResults objectAtIndex:indexPath.row];
UILabel *game = (UILabel*) [cell viewWithTag:200];
game.text = [NSString stringWithFormat:@"V.s %@", [searchResult objectForKey:@"opponent"]];
UILabel *points = (UILabel*) [cell viewWithTag:201];
points.text = [NSString stringWithFormat:@"Points: %@G, %@A, %@Pts, %@, %@Hits, %@PPG",[searchResult objectForKey:@"goals"], [searchResult objectForKey:@"assists"], [searchResult objectForKey:@"totalpoints"], [searchResult objectForKey:@"plusminus"], [searchResult objectForKey:@"hits"], [return cell; objectForKey:@"ppg"]];
UILabel *date = (UILabel*) [cell viewWithTag:250];
date.text = [searchResult objectForKey:@"date"];
UILabel *name = (UILabel*) [cell viewWithTag:203];
name.text = [searchResult objectForKey:@"name"];
}
return cell;