前言:我有两个字典(来自.plists),其中包含示例数据,在这种情况下是跑回和四分卫。
各个位置分为两个部分,并用标题分隔。
当我搜索时(我目前只搜索rb数组作为概念证明,直到找到搜索两个词典或组合它们的方法)。
问题:当我搜索已知的跑步" Clinton Portis"时,查看表显示过滤结果,Clinton Portis是唯一可用的选择。
一切顺利,直到我点击它将cell.textlabel
返回到另一个视图控制器上的标签。
在这种情况下,另一个视图控制器返回另一个QBArray中的第一个单元格' Tom Brady'。
我知道我可能会遗漏某些内容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
请看下面的代码,请让我知道我做错了什么(以及你在路上发现的任何其他愚蠢的错误)
提前谢谢!
//
// RootViewControllerTableViewController.m
// FFCB
//
#import "RootViewControllerTableViewController.h"
#import "ViewController.h"
@interface RootViewControllerTableViewController ()
@property (nonatomic, copy) NSMutableArray *searchResults;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@end
@implementation RootViewControllerTableViewController{
NSArray *justPlayerKeys;
NSDictionary *qbDictionary;
//NSMutableArray *justQBData;
NSArray *justRBKeys;
NSDictionary *rbDictionary;
//NSMutableArray *justRBData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_searchResults = [[NSMutableArray alloc]init];
_searchController = [[UISearchDisplayController alloc]init];
_searchController.searchResultsDataSource = self;
[self.tableView setContentInset:UIEdgeInsetsMake(20,self.tableView.contentInset.left,self.tableView.contentInset.bottom,self.tableView.contentInset.right)]; // moves search bar from top
NSURL *url = [[NSBundle mainBundle] URLForResource:@"QBS"withExtension:@"plist"];
qbDictionary = [NSDictionary dictionaryWithContentsOfURL:url];
justPlayerKeys = qbDictionary.allKeys;
NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"RBS"withExtension:@"plist"];
rbDictionary = [NSDictionary dictionaryWithContentsOfURL:url2];
justRBKeys = rbDictionary.allKeys;
//int i;
// for(i=0; i < justPlayerKeys.count; i++){
// NSMutableString *outputQB;
// outputQB = [justPlayerKeys objectAtIndex:i];
//justQBData[i] = [qbDictionary objectForKey:outputQB];
// }
/* int j;
for(j=0; i < justRBKeys.count; j++){
NSMutableString *outputRB;
outputRB = [justRBKeys objectAtIndex:j];
//justRBData[j] = [rbDictionary objectForKey:outputRB];
}*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
} else {
return 2;
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;
} else {
if (section == 0){
return @"Quarterbacks";
}
else{
return @"Runningbacks";
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [_searchResults count];
} else {
if(section ==0){
return qbDictionary.count;
}
else {
return rbDictionary.count;
}
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
_searchResults = [justRBKeys filteredArrayUsingPredicate:resultPredicate];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if(tableView == self.searchDisplayController.searchResultsTableView){
cell.textLabel.text = _searchResults[indexPath.row];
}
else{
if(indexPath.section == 0){
cell.textLabel.text = justPlayerKeys[indexPath.row];
cell.detailTextLabel.text =qbDictionary[justPlayerKeys[indexPath.row]];
//cell.detailTextLabel.text =justQBData[indexPath.row];
}
else{
cell.textLabel.text = justRBKeys[indexPath.row];
cell.detailTextLabel.text =rbDictionary[justRBKeys[indexPath.row]];
//cell.detailTextLabel.text =justRBData[indexPath.row];
}
}
cell.accessoryType = UITableViewCellAccessoryDetailButton;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.searchDisplayController.searchResultsTableView){
[self.delegate didSelectWith:self player:[self.tableView cellForRowAtIndexPath:indexPath].textLabel.text];
}else{
[self.delegate didSelectWith:self player:[self.tableView cellForRowAtIndexPath:indexPath].textLabel.text];
}
}
// use the image in the next window using view controller instance of that view.
-(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
@end