2013-08-05 4 views
0

Donc j'essaye de faire une application qui montre des noms de personnes dans une vue de table et sur le robinet se déplace au contrôleur de vue suivant qui montre une image de la personne. Cependant, lorsque j'ajoute la barre de recherche sur la vue de la table; Je ne semble pas avoir raison.Comment faire pour que SearchBar dans tableview fonctionne?

Qu'est-ce que je fais mal ici? Le code compile et affiche sur le simulateur, mais quand je clique sur l'un des boutons, il me donne les erreurs que je déteste le plus (Discussion 1: Signal SIGABRT)

Voici mon code pour la table View Controller

#import "PhotoTableViewController.h" 
#import "Photo.h" 
#import "DisplayViewController.h" 

@interface PhotoTableViewController() 

@end 

@implementation PhotoTableViewController 
@synthesize photoSearchBar, showPhotos, filteredPhotos; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    showPhotos = [NSArray arrayWithObjects: 
          [Photo photoofname:@"Main" filename:@"photo1.jpg" notes:@"Amazing Road Bike"], 
          [Photo photoofname:@"Back" filename:@"photo3.jpg" notes:@"this is the back"], nil]; 
    [self.tableView reloadData]; 

    self.filteredPhotos = [NSMutableArray arrayWithCapacity:[showPhotos count]]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [filteredPhotos count]; 
    } else { 
     return [showPhotos count]; 
    } 
} 

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

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    Photo *photo = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     photo = [filteredPhotos objectAtIndex:indexPath.row]; 

    }else 
    { 
     photo = [showPhotos objectAtIndex:indexPath.row]; 
    } 

    cell.textLabel.text = photo.name; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    return cell; 
} 

#pragma mark Content Filtering 

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { 
    // Update the filtered array based on the search text and scope. 
    // Remove all objects from the filtered search array 
    [self.filteredPhotos removeAllObjects]; 
    // Filter the array using NSPredicate 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText]; 
    filteredPhotos = [NSMutableArray arrayWithArray:[showPhotos filteredArrayUsingPredicate:predicate]]; 
} 

#pragma mark - UISearchDisplayController Delegate Methods 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    // Tells the table data source to reload when text changes 
    [self filterContentForSearchText:searchString scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { 
    // Tells the table data source to reload when scope bar selection changes 
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; 
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

#pragma mark - TableView Delegate 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Perform segue to candy detail 
    [self performSegueWithIdentifier:@"candyDetail" sender:tableView]; 
} 

#pragma mark - Segue 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"photoDetail"]) { 
     UIViewController *candyDetailViewController = [segue destinationViewController]; 
     // In order to manipulate the destination view controller, another check on which table (search or normal) is displayed is needed 
     if(sender == self.searchDisplayController.searchResultsTableView) { 
      NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; 
      NSString *destinationTitle = [[filteredPhotos objectAtIndex:[indexPath row]] name]; 
      [candyDetailViewController setTitle:destinationTitle]; 
     } 
     else { 
      NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
      NSString *destinationTitle = [[showPhotos objectAtIndex:[indexPath row]] name]; 
      [candyDetailViewController setTitle:destinationTitle]; 
     } 

    } 
} 

aussi c'est le code pour ma classe Objective C appelé photo

#import "Photo.h" 

@implementation Photo 

@synthesize name,filename,notes; 

+(id) photoofname: (NSString*)name filename:(NSString*)filename notes:(NSString*)notes{ 
    Photo *newPhoto = [[Photo alloc]init]; 
    newPhoto.name = name; 
    newPhoto.filename = filename; 
    newPhoto.notes = notes; 
    return newPhoto; 
} 

@end 
+0

pouvez-vous poster le journal des exceptions? – Woodstock

+0

Pouvez-vous s'il vous plaît poster les messages de journal? – nithinbhaktha

+0

@JohnWoods Merci pour vos contributions; Il s'avère qu'il s'agissait d'une petite erreur sur myside en nommant l'identifiant de réutilisation du segue. Bien que la transition de vue de table vers image de vue fonctionne maintenant; les images n'apparaissent pas sur la vue de l'image. Actuellement, je n'ai pas d'erreurs lors de la compilation et fonctionne bien; juste que les images ne se chargent pas/apparaissent. Pourriez-vous s'il vous plaît examiner le code avec cet aspect en tête? – aceprogramer

Répondre

1

Eh bien, juste en regardant le code ce que je peux suggérer, vous est, d'abord supprimer cet appel à la méthode prepareForSegue appelé délégué UITableView méthode, didSelectForR owAtIndexPath. Vous êtes en train de surcharger prepareForSegue, donc dans votre storyboard, vous devriez avoir une cellule prototype à partir de laquelle vous devez faire un Ctrl-glisser vers le contrôleur de destination et la coupler en conséquence. C'est un concept de base. Vous avez toujours un problème? Laissez-nous voir vos messages de la console quand il se bloque.

+0

Merci pour vos contributions; Il s'avère qu'il s'agissait d'une petite erreur sur myside en nommant l'identifiant de réutilisation du segue. Bien que la transition de vue de table vers image de vue fonctionne maintenant; les images n'apparaissent pas sur la vue de l'image. Actuellement, je n'ai pas d'erreurs pendant la compilation et fonctionne bien; juste que les images ne se chargent pas/apparaissent. Pourriez-vous s'il vous plaît examiner le code avec cet aspect en tête? – aceprogramer

+1

Eh bien, votre code me dit que vous avez toutes vos images dans des photos filtrées encapsulées dans un objet Photo. Mais où est la ligne de code où vous définissez cette image à une instance de l'image vue? Comme dans, Si vous avez un objet UIImageView, vous êtes censé utiliser la méthode setImage sur cet objet avec une instance de UIImage. – nithinbhaktha

+0

Woohoo! Merci beaucoup. Je l'ai juste au travail. Pouvez-vous également aider avec celui-ci: http://stackoverflow.com/questions/18074366/how-to-generate-custom-table-view-based-on-textfield-input – aceprogramer

Questions connexes