Lorsque mon utilisateur cherche quelque chose à l'intérieur d'une TableView, lorsque aucun résultat n'est renvoyé, mon application indique l'espace réservé standard "Aucun résultat" dans la tableView. Cela dit, quand aucun résultat n'existe, je souhaite renvoyer une cellule peuplée (cellule remplie de données par défaut). Comment puis-je accomplir cela? J'ai essayé ci-dessous, mais je n'ai toujours pas renvoyé «aucun résultat»?
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
if ([searchResults count] == 0) {
return 1;
} else {
return [searchResults count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NetworkTableIdentifier = @"sidebarCell";
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = nib[0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSDictionary *userName = searchResults[indexPath.row];
NSString *first = userName[@"first name"];
NSString *last = userName[@"last name"];
[[cell username] setText:[NSString stringWithFormat:@"%@ %@", first, last]];
NSDictionary *userlast = searchResults[indexPath.row];
[[cell lastName] setText:userlast[@"last name"]];
NSDictionary *userBio = searchResults[indexPath.row];
[[cell userDescription] setText:userBio[@"userbio"]];
NSString *area = userName[@"neighbourhood"];
NSString *city = userName[@"city"];
[[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", area, city]];
NSString *profilePath = searchResults[indexPath.row][@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
if ([searchResults count] == 0) {
NSLog(@"SEARCH RESULTS ARE %@", searchResults);
[[cell username] setText:[NSString stringWithFormat:@"%@", self.searchBar.text]];
[[cell lastName] setText:userlast[@""]];
[[cell userDescription] setText:@"This friend is not on the app (yet!) Tap to invite them."];
[[cell areaLabel] setText:[NSString stringWithFormat:@""]];
NSString *profileDefault = @"http://url.com/user.png";
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profileDefault]];
return cell;
}
return cell;
}
3 Réponses :
Je ne recommande pas vraiment de faire cela, car vous devriez renvoyer une liste vide, s'il n'y a pas de résultats de recherche. Cela est cohérent avec les directives d'interface utilisateur. Mais si vous insistez, vous pouvez créer un objet par défaut et initialiser votre matrice de rechercheRésultats avec cet objet et renvoyer 1 de la méthode numériquesOrows. Quelque chose comme ceci: et, vous pouvez simplifier grandement simplifier votre code celltfornordexPath comme suit: p> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NetworkTableIdentifier = @"sidebarCell";
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
//Note, I like to name my local variables the same as the dictionary keys just to eliminate any confusion
NSDictionary *userObject = [searchResults objectAtIndex:indexPath.row];
NSString *first_name = [userObject objectForKey:@"first name"];
NSString *last_name = [userObject objectForKey:@"last name"];
NSString *userbio = [userObject objectForKey:@âuserbioâ];
NSString *neighbourhood = [userObject objectForKey:@âneighbourhoodâ];
NSString *city = [userObject objectForKey:@âcityâ];
NSString *photo_path = [userObject objectForKey:@âphoto_pathâ];
[[cell username] setText:[NSString stringWithFormat:@"%@ %@", first_name, last_name]];
[[cell lastName] setText:last_name];
[[cell userDescription] setText:userbio];
[[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", neighbourhood, city]];
[[cell usermini] sd_setImageWithURL:[NSURL URLWithString:photo_path]];
}
return cell;
}
J'ai fait quelque chose comme ça dans mon application. C'est moche et je ne vous recommande pas de suivre de cette façon. Je l'ai fait seulement parce que j'étais un peu paresseux des mises en page et de placer l'espace réservé au bon endroit dans la hiérarchie de vue et de gérer toutes ces situations cachées / spectacles. Mon contrôleur de vue a une hiérarchie de vues très complexe et la vue sur la table était celle qui a déjà eu tout ce dont j'avais besoin (redimensionner automatiquement lorsque l'état ou la barre d'outils affiche). P>
Qu'est-ce que je vous suggère de masquer la vue sur la table lorsqu'il y a un résultat de recherche vide et remplacez-le avec votre espace réservé. P>
Essayez ceci, cela fonctionne:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if (arrData.count>0)
{
self.TblView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
self.TblView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.TblView.bounds.size.width, self.TblView.bounds.size.height)];
noDataLabel.text = @"No Results";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
self.TblView.backgroundView = noDataLabel;
self.TblView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
Je peux vous dire comment vous débarrasser des résultats et montrer une table vide, que je pense est une interface beaucoup plus agréable.
Savoir déjà comment faire ça - et pas tout à fait ce que je cherche @matt.
:) OK, pas de problème.
Il semble que votre numéro de code est incomplet ou au moins, vous devez obtenir des erreurs de compilateur sur la méthode numérosofrowsection ().
@onnoweb oui. Et quand searchResults est vide, vous retournerez 1. Dans Cellfornor, il s'écrasera à NSDictionary * UserLast = [Recherche ObjectAindex: IndexPath.row];