#import <UIKit/UIKit.h>
@interface pinpaiview : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *atable;
NSArray *array1;
NSArray *array2;
UISearchDisplayController *displayController;
UISearchBar *searchBar;
NSMutableArray * all;
NSArray *resultTableView; //搜索的结果赋到新的数组
}
-(void)backmethoed;
@property (nonatomic,retain) NSArray *array1;
@property (nonatomic,retain) NSArray *array2;
@property (nonatomic ,retain) NSArray *resultTableView;
@property (nonatomic,assign) NSMutableArray *all;
@property (nonatomic,retain) UISearchDisplayController *displayController;
@property (nonatomic,retain) UISearchBar *searchBar;
@property (nonatomic ,retain) UITableView *atable;
@end
//实现部分
self.atable=[[UITableView alloc] initWithFrame:CGRectMake(0, 46, 320, 460) style:UITableViewStyleGrouped];
atable.delegate=self;
atable.dataSource=self;
self.array1=[[NSArray alloc] initWithObjects:@"abc",@"bcd",@"cde", nil];
self.array2=[[NSArray alloc] initWithObjects:@"def",@"efg", nil];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,46, 320.0f, 44.0f)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.keyboardType = UIKeyboardTypeAlphabet;
self.atable.tableHeaderView = self.searchBar;
displayController=[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
displayController.searchResultsDelegate=self;
displayController.searchResultsDataSource=self;
[self.view addSubview:atable];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section==0) {
return @"1";
}
if (section==1) {
return @"2";
}
return nil;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *array4=[[NSArray alloc] initWithObjects:@"USA",@"china", nil];
return array4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger a=0;
//过滤关键字
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",self.searchBar.text];
NSLog(@"%@",searchBar.text);
self.all = [array1 mutableCopy];
[self.all addObjectsFromArray:array2];
NSLog(@"%@",self.all);
将搜索的结果赋值给新的数组
self.resultTableView = [self.all filteredArrayUsingPredicate:resultPredicate ] ;
NSLog(@"%@",self.resultTableView);
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
a=[self.resultTableView count];
}
else{
if (section==0) {
a=[array1 count];
}
if (section==1) {
a=[array2 count];
}
}
return a;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([tableView isEqual:self.displayController.searchResultsTableView]) {
return 1;
}
else
{
return 2;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *stringcell = @"cell";//这个就是那个标识符
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stringcell];//给这个cell作为一个标识
//如果原来没有cell,或者被释放没有了,那么就重新新建一个cell并且标识一下
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:stringcell] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
cell.textLabel.text=[self.resultTableView objectAtIndex:indexPath.row];
}
else{
if (indexPath.section==0)
{
cell.textLabel.text=[self.array1 objectAtIndex:indexPath.row];
}
if (indexPath.section==1)
{
cell.textLabel.text= [self.array2 objectAtIndex:indexPath.row];
}
}
return cell;
}
浙公网安备 33010602011771号