我尝试制作一个小程序来了解有关搜索关于搜索栏的 tableview 的更多信息。与此同时,我正在第三周尝试这样做。我在互联网上以不同的方式找到了许多示例。最多使用搜索显示控制器,但这种方式仅适用于视图控制器。我更喜欢一种也适用于 uiview 的方式,因此我使用搜索栏委托方法来过滤我的数据字典。通常我会尝试自己处理这些事情。但这是我的克星。我真的找不到解决这个问题的方法。任何人都可以帮我处理我的代码吗?这里是。
#import "FilterDemoTableViewController.h"
@implementation FilterDemoTableViewController
@synthesize filteredTableData;
@synthesize searchBar;
@synthesize isFiltered;
@synthesize tableContents;
@synthesize Keys;
- (void)viewDidLoad
{
[super viewDidLoad];
searchBar.delegate = (id)self;
//-----------------------My TableView Data ------------------------------
NSArray *array1 = [[NSArray alloc]initWithObjects:@"Berlin",@"München",@"Stuttgart",nil];
NSArray *array2 = [[NSArray alloc]initWithObjects:@"Paris",@"Bordeaux",@"Marseille",@"Toulouse",nil];
NSArray *array3 = [[NSArray alloc]initWithObjects:@"London",@"Portsmouth",@"Oxford",@"York",@"Dover",nil];
NSArray *array4 = [[NSArray alloc]initWithObjects:@"Rom" ,@"Genua",@"Mailand",@"Florenz",nil];
NSArray *array5 = [[NSArray alloc]initWithObjects:@"Madrid",@"Barcelona",@"Toledo",@"Saragossa",@"Pamplona",nil];
NSDictionary *dictionary =[[NSDictionary alloc]initWithObjectsAndKeys:array1,@"Deutschland",array2,@"Frankreich",array3,@"Großbritannien",array4,@"Italien",array5,@"Spanien",nil];
self.tableContents = dictionary;
self.Keys = [self.tableContents allKeys];
//--------------------------------------------------------------------------
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.isFiltered) {
return [filteredTableData count];
} else {
return [Keys count];}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *listData =[self.tableContents objectForKey:[self.Keys objectAtIndex:section]];
int rowCount;
if(self.isFiltered)
rowCount = filteredTableData.count;
else
rowCount = [listData count];
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSDictionary* sectionDictionary;
if (isFiltered) {
sectionDictionary = [filteredTableData objectAtIndex:indexPath.section];
} else {
sectionDictionary = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];
}
NSArray* sectionEntries = [self.tableContents objectForKey:[self.Keys objectAtIndex:indexPath.section]];
cell.textLabel.text = [sectionEntries objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
//I think here is something wrong but i don't know what
isFiltered = true;
filteredTableData = [[NSMutableArray alloc] init];
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in tableContents) //dictionary read
{
NSArray *array = [dictionary objectForKey:Keys]; //section of dictionary read
[searchArray addObjectsFromArray:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:text options:NSCaseInsensitiveSearch];
if (titleResultsRange.length != 0)
[filteredTableData addObject:sTemp];
}
}
[self.tableView reloadData];
}
- (void)viewDidUnload{
[self setSearchBar:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end