iOS搜索栏无法链接到正确的结果

时间:2014-01-23 21:55:37

标签: ios iphone uitableview uisearchdisplaycontroller

每当我从搜索栏中搜索某些内容时,我都会得到正确的结果。当我点击这些结果时,它会将我链接到原始结果将我链接到的相同位置。换句话说,我有老师a-e,我输入'e',只获得结果'e',但是当我点击那个单元格时,它会链接到老师'a'个人资料。 这是我到目前为止所拥有的。

#import <UIKit/UIKit.h>

@interface ListTableViewController : UITableViewController

@end

---


#import "ListTableViewController.h"
#import "DetailsViewController.h"

@interface ListTableViewController () <UISearchDisplayDelegate>

@property (strong, nonatomic) NSArray *className;

@property (strong, nonatomic) NSArray *teacherName;
@property (strong, nonatomic) NSArray *blockNumber;
@property (strong, nonatomic) NSArray *myNew;
@property (strong, nonatomic) NSArray *searchResults;

@end

@implementation ListTableViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.className = [NSArray arrayWithObjects:@"Biology",@"English III",@"Chemistry",@"Algebra II",@"Morality", nil];
    self.teacherName = [NSArray arrayWithObjects:@"Teacher A",@"Teacher B",@"Teacher C",@"Teacher D",@"Teacher E", nil];
    self.blockNumber = [NSArray arrayWithObjects:@"B1",@"B3",@"B6",@"B2",@"B1", nil];



    NSMutableArray *combinedArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self.className count]; i++)
    {
        NSString *combinedString = [NSString stringWithFormat:@"%@ | %@ | %@",[self.className objectAtIndex:i],[self.teacherName objectAtIndex:i],[self. blockNumber objectAtIndex:i]];
        [combinedArray addObject:combinedString];
    }
    self.myNew = combinedArray;
}

- (void)filterContentForSearchText: (NSString *) searchText
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchText];
    self.searchResults = [self.myNew filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];
    return YES;
}

#pragma mark - Table view data source

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

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

}

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

    if (tableView == self.tableView) {
        cell.textLabel.text = [self.myNew objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    }
    return cell;
}



#pragma mark - Table view delegate

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showDetails"]) {
        DetailsViewController *dvc = segue.destinationViewController;

        NSIndexPath *indexPath = nil;

        if ([self.searchDisplayController isActive]) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];

            dvc.sendLabel = [self.searchResults objectAtIndex:indexPath.row];
            dvc.teachersendLabel = [self.teacherName objectAtIndex:indexPath.row];
            return;
        } else{
            indexPath = [self.tableView indexPathForSelectedRow];
            dvc.sendLabel = [self.myNew objectAtIndex:indexPath.row];
            dvc.teachersendLabel = [self.teacherName objectAtIndex:indexPath.row];
            return;
        }
    }
}

@end

在我的DetailsViewController

#import <UIKit/UIKit.h>

@interface DetailsViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) NSString *sendLabel;

@property (weak, nonatomic) IBOutlet UILabel *teacherlabel;
@property (strong, nonatomic) NSString *teachersendLabel;

@end

---

@implementation DetailsViewController
@synthesize label;


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.teacherlabel.text = [NSString stringWithFormat:@"%@", self.teachersendLabel];
    self.label.text = [NSString stringWithFormat:@"%@", self.sendLabel];

}

@end

1 个答案:

答案 0 :(得分:0)

查看您的代码似乎没有任何问题。这是我能想到的两件事:

1)我不确定你是如何显示'main'tableView和搜索结果的。可能是你的触摸实际上是由'main'tableView处理的?如果您将两个表对齐在一起,并且底部的一个仍然可见,并且当搜索一个'isActive'时userInteractionEnabled设置为YES,则可能会发生这种情况。在这种情况下,视图层次结构应类似于:

- UIView
    - UITableView (main)
    - UITableView (search)

2)在-[UITableView indexPathForSelectedRow]中使用prepareForSegue:sender:。如果您使用的是Storyboard,则发件人是选定的单元格。您可能想要检查发件人是实际单元格还是indexPath isKindOfClass。如果发件人是indexPath,您可以使用它,如果它是一个单元格,您可以调用方法-[UITableView indexPathForCell:]。使用这种方法,你可以确保你的segue实际上是在触发正确的事件(例如,你可以通过编程方式选择一个单元格,但这不会触发一个segue,你可以稍后决定调用-performSegueWithIdentifier:sender:这会破坏你的实现)。