如何在tableview中使搜索栏工作?

时间:2013-08-05 06:09:26

标签: iphone objective-c uitableview uisearchbar

所以我试图创建一个在tableview中显示人物名称的应用程序,然后点击移动到显示该人物图像的下一个视图控制器。 但是当我在表格视图中添加搜索栏时;我好像没错。

我在这里做错了什么? 代码在模拟器上编译和显示,但是当我点击任何按钮时,它给出了我最讨厌的错误(线程1:信号SIGABRT)

这是我的表视图控制器代码

#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];
        }

    }
}

这也是我的Objective C Class的代码,名为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

1 个答案:

答案 0 :(得分:1)

好吧,只需查看代码我可以建议你,首先删除对UITableView的委托方法didSelectForRowAtIndexPath中调用的prepareForSegue方法的调用。 你正在重写prepareForSegue,所以在你的故事板中你应该有一个原型单元格,你必须按住它来拖动到目标控制器并相应地进行调整。这是一个基本概念。还有问题吗?让我们在崩溃时看到您的控制台消息。