将搜索栏单元格标签链接到视图控制器

时间:2014-01-15 21:15:29

标签: xcode search view controller

我正在尝试在Xcode中创建一个搜索栏,所以当你搜索一个项目时,你可以点击搜索结果单元格然后它会转到一个视图控制器,但它现在正在做的就是去海景控制器并只显示我点击的单元格的标签。

这是我的.m文件

@interface ListTableViewController () <UISearchDisplayDelegate>

@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) NSArray *searchResults;

@end

@implementation ListTableViewController

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

- (void)viewDidLoad
{
[super viewDidLoad];

self.dataArray = [NSArray arrayWithObjects:@"Tomb Raider", @"Little Big Planet",     @"Unchanted 3", @"BlackOps 2", nil];
}

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

- (void)filterContentForSearchText: (NSString *) searchText
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchText];
self.searchResults = [self.dataArray 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.dataArray 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.dataArray 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];
        return;
    } else{
        indexPath = [self.tableView indexPathForSelectedRow];
        dvc.sendLabel = [self.dataArray objectAtIndex:indexPath.row];
        return;
    }
}
}


@end

谢谢,任何事情都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您必须实现此委托方法:

// Method returns the clicked row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   YourPushedViewController *ypVC = [[YourPushedViewController alloc] initWithNibName:@"YourPushedViewController" bundle:nil];
  ypVC.selectedText = myResultArray[indexPath.row]; 


   [self.navigationController pushViewController:ypVC animated:YES]; 

}

该方法是选择行并将选定结果传递给另一个视图的响应。

这是给你的提示:Passing Data between View Controllers