2014-01-15 40 views
0

我正在嘗試在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 

謝謝,什麼會有所幫助。

+0

我不想寫的你,而不是所有的控制器代碼,但我認爲,如果你閱讀本教程http://www.appcoda.com/how-to-add -search-bar-uitableview /你應該找到答案。 – Robert

+0

我已經試過這個教程,它有同樣的問題。 –

+0

你不能推新視圖控制器,還是什麼?認真我不瞭解你。請指定您的問題 – Robert

回答

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

+0

好吧,我試試看,謝謝! –

+0

我試過這個,它一直說「使用未聲明的標識符'myresultarray'」 我遇到麻煩的部分是 - ypVC.ypvcText = myResultArray [indexPath.row]; –

+0

當然,因爲myResultArray它是我自己的變量,所以您必須通過您的自定義數組交換此變量。而YourPushedViewController是我的ViewController,你必須添加到項目你的自定義ViewController與公共屬性selectedText – Robert

相關問題