1

我有一个带有搜索栏的 UITableView,搜索栏可以工作,但结果不会链接到我的详细视图。下面的代码:

#import "firstTableViewController.h"
#import "detailViewCell.h"
#import "DetailViewController.h"

@interface firstTableViewController ()

@end

@implementation firstTableViewController
@synthesize craftingItems1;
@synthesize bigImages;
@synthesize theText;

@synthesize searchDisplayController;
@synthesize searchResults;

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

- (void)viewDidLoad
{
[super viewDidLoad];
self.craftingItems1 = [[NSArray alloc] initWithObjects:
                       @"Item 1",
                       @"Item 2",
                       @"Item 3",
                       @"Item 4",
                       @"Item 5", nil];


self.theText = [[NSArray alloc] initWithObjects:
                @"Description 1", 
                @"Description 2", 
                @"Description 3", 
                @"Description 4", 
                @"Description 4", nil];


self.bigImages = [[NSArray alloc] initWithObjects:
                  @"Image 1.png", 
                  @"Image 2.png", 
                  @"Image 3.png", 
                  @"Image 4.png", 
                  @"Image 5.png", nil];


}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:      (NSInteger)section
{
// Return the number of rows in the section.
NSInteger rows = 0;

if ([tableView 
     isEqual:self.searchDisplayController.searchResultsTableView]){
    rows = [self.searchResults count];
}
else{
    rows = [self.craftingItems1 count];
}

return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView                        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"tableCell";

UITableViewCell *cell = [tableView 
                                dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] 
             initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

/* Configure the cell. */
if ([tableView  isEqual:self.searchDisplayController.searchResultsTableView]){
    cell.textLabel.text = 
    [self.searchResults objectAtIndex:indexPath.row];


}
else{
    cell.textLabel.text =
    [self.craftingItems1 objectAtIndex:indexPath.row];


}

return cell;

}

// Configure the cell...







-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowCrafting"])
{
    DetailViewController *theDetailViewController = 
    [segue destinationViewController];

    NSIndexPath *myIndexPath = [self.tableView 
                                indexPathForSelectedRow];

    theDetailViewController.craftingModel = [[NSArray alloc]
                                             initWithObjects:   [self.craftingItems1
                                                                objectAtIndex:[myIndexPath row]],
                                             [self.theText objectAtIndex:   [myIndexPath row]],
                                             [self.bigImages    objectAtIndex:[myIndexPath row]],
                                             nil];





}
}


- (void)filterContentForSearchText:(NSString*)searchText 
                         scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate 
                                predicateWithFormat:@"SELF contains[cd]     %@",
                                searchText];

self.searchResults = [self.craftingItems1   filteredArrayUsingPredicate:resultPredicate];
}

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

return YES;
}



- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar    text] 
                           scope:[[self.searchDisplayController.searchBar   scopeButtonTitles]
                                  objectAtIndex:searchOption]];

return YES;
}

@end

详细视图通常与表格一起使用,但是当使用搜索时,项目列表不会链接到详细视图。

4

0 回答 0