0

Whenever I search for something from a search bar, I get the correct results. When I click on those results, it links me to the same place that the original results would have linked me to. In other words, I have teacher a-e, I type in 'e', and get only the result 'e', but when I click on that cell, it links me to the teacher 'a' profile. Here is what I have so far.

#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

In my 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
Brian
  • 14,610
  • 7
  • 35
  • 43
  • Put a breakpoint on the `if ([self.searchDisplayController isActive])` line in `-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender` and make sure you're getting into the expected part of the if/else statement. – Brian Jan 23 '14 at 22:49
  • Note to editor: @Kevin, *we never modify code* when editing, only indentation and formating. – brasofilo Jan 23 '14 at 23:07

1 Answers1

0

Looking at your code it wouldn't seem there to be any problem. The are only two things I can think of:

1) I'm not sure how you're displaying the 'main' tableView and the search results one. Might it be that your touches are actually getting handled by the 'main' tableView? This might happen if you have the two tables aligned on top of each other and the bottom one is still visible and with userInteractionEnabled set to YES when the search one 'isActive'. In this case the view hierarchy should look similar to this:

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

2) the use of -[UITableView indexPathForSelectedRow] in prepareForSegue:sender:. If you're using Storyboard the sender is the selected cell. You may want to check that the sender is an actual cell or an indexPath isKindOfClass. If the sender is an indexPath you can use it, if it's a cell you can call the method -[UITableView indexPathForCell:]. Using this approach you make sure your segue is actually triggering for the right event (e.g. you can programmatically select a cell, but this won't fire a segue and you can later decide to call -performSegueWithIdentifier:sender: and this would break your implementation).

Gianluca Tranchedone
  • 3,598
  • 1
  • 18
  • 33