表搜索segue不匹配表segue

时间:2014-05-27 18:19:20

标签: ios objective-c uitableview

我已经在tableview上实现了“搜索栏和搜索显示控制器”,并且可以在我输入时在新表中查看我的结果。我的问题是我在搜索列表中点击的单元格不会导致与表格中的单元格相同的详细信息页面。我知道问题在于我的segue中的indexPath.row,但我不知道如何解决这个问题。

我想我的问题归结为,我如何让两张桌子都指向相同的详细信息页面?

以下是我的代码:

#import "BuildingsViewController.h"
#import "BuildingsMapViewController.h"

static NSString *CellIdentifier = @"buildingsCell";

@interface BuildingsViewController ()

@property (nonatomic) NSArray *keys;
@property (nonatomic, strong) NSMutableArray *tempMutableArray;

@end

@implementation BuildingsViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelector:@selector(retrieveBuildings)];

    [self setTempMutableArray:[[NSMutableArray alloc] init]];
    [self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class]
                                                forCellReuseIdentifier:CellIdentifier];
}

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

#pragma mark - TableView Setup

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];

    } else {
        return [self.buildingsArray count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:CellIdentifier];

        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];

    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        NSDictionary *tempDict = nil;
        NSString *tempString = nil;

        tempDict = [self.buildingsArray objectAtIndex:indexPath.row];
        tempString = [tempDict objectForKey:@"buildingTitle"];

        [cell.textLabel setText:tempString];
    }
    return cell;
}

#pragma mark - Helper Methods

-(void)retrieveBuildings
{
    PFQuery *retrieveBuildings = [PFQuery queryWithClassName:@"buildingsList"];
    [retrieveBuildings orderByAscending:@"buildingTitle"];
    [retrieveBuildings findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.buildingsArray = [[NSArray alloc] initWithArray:objects];
        }
        [self.tableView reloadData];
    }];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = nil;

    [self.tempMutableArray removeAllObjects];
    for (NSDictionary *tempDict in self.buildingsArray) {
        NSString *tempString = nil;
        tempString = [tempDict objectForKey:@"buildingTitle"];
        [self.tempMutableArray addObject:tempString];
    }

    resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
    self.searchResults = [self.tempMutableArray filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"showMapDetail" sender: self];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showMapDetail"]) {
        BuildingsMapViewController *destViewController = segue.destinationViewController;

        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            destViewController.title = [self.searchResults objectAtIndex:indexPath.row];

            PFObject *object = [self.buildingsArray objectAtIndex:indexPath.row];
            self.geoPoint = [object objectForKey:@"GeoPoint"];

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            NSDictionary *tempDict = nil;

            tempDict = [self.buildingsArray objectAtIndex:indexPath.row];
            NSLog(@"%@", tempDict);
            destViewController.title = [tempDict objectForKey:@"buildingTitle"];

            PFObject *object = [self.buildingsArray objectAtIndex:indexPath.row];
            self.geoPoint = [object objectForKey:@"GeoPoint"];
        }

        destViewController.geoPoint = self.geoPoint;
        destViewController.annotationLat = self.geoPoint.latitude;
        destViewController.annotationLong = self.geoPoint.longitude;

    }

}

@end

更新的代码(在segue上崩溃):

#import "BuildingsViewController.h"
#import "BuildingsMapViewController.h"

static NSString *CellIdentifier = @"buildingsCell";

@interface BuildingsViewController ()

@property (nonatomic) NSArray *keys;

@property (nonatomic, strong) NSMutableArray *tempMutableArray;

@end

@implementation BuildingsViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelector:@selector(retrieveBuildings)];

    [self setTempMutableArray:[[NSMutableArray alloc] init]];
    [self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class]
                                                forCellReuseIdentifier:CellIdentifier];
}


#pragma mark - TableView Setup

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];

    } else {
        return [self.buildingsArray count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:CellIdentifier];

        NSDictionary *tempDict = nil;
        NSString *tempString = nil;

        tempDict = [self.searchResults objectAtIndex:indexPath.row];
        tempString = [tempDict objectForKey:@"buildingTitle"];
        cell.textLabel.text = tempString;


    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        NSDictionary *tempDict = nil;
        NSString *tempString = nil;

        tempDict = [self.buildingsArray objectAtIndex:indexPath.row];
        tempString = [tempDict objectForKey:@"buildingTitle"];

        [cell.textLabel setText:tempString];
    }
    return cell;
}

#pragma mark - Helper Methods

-(void)retrieveBuildings
{
    PFQuery *retrieveBuildings = [PFQuery queryWithClassName:@"buildingsList"];
    [retrieveBuildings setLimit:300];
    [retrieveBuildings orderByAscending:@"buildingTitle"];
    [retrieveBuildings findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.buildingsArray = [[NSArray alloc] initWithArray:objects];
        }
        [self.tableView reloadData];
    }];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = nil;

    [self.tempMutableArray removeAllObjects];
    for (NSDictionary *tempDict in self.buildingsArray) {
        NSString *tempString = nil;
        tempString = [tempDict objectForKey:@"buildingTitle"];
        [self.tempMutableArray addObject:tempString];
    }

    resultPredicate = [NSPredicate predicateWithFormat:@"buildingTitle contains[cd] %@", searchText];
    self.searchResults = [self.buildingsArray filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"showMapDetail" sender: self];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showMapDetail"]) {
        BuildingsMapViewController *destViewController = segue.destinationViewController;

        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            destViewController.title = [self.searchResults objectAtIndex:indexPath.row];

            PFObject *object = [self.searchResults objectAtIndex:indexPath.row];
            self.geoPoint = [object objectForKey:@"GeoPoint"];

        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            NSDictionary *tempDict = nil;

            tempDict = [self.buildingsArray objectAtIndex:indexPath.row];
            NSLog(@"%@", tempDict);
            destViewController.title = [tempDict objectForKey:@"buildingTitle"];

            PFObject *object = [self.buildingsArray objectAtIndex:indexPath.row];
            self.geoPoint = [object objectForKey:@"GeoPoint"];
        }

        destViewController.geoPoint = self.geoPoint;
        destViewController.annotationLat = self.geoPoint.latitude;
        destViewController.annotationLong = self.geoPoint.longitude;

    }

}

@end

0 个答案:

没有答案