首先要在xib中拖動一個searchBar VC 也不用連方法的線。。。
UISearchBarDelegate,UISearchDisplayDelegate>
///搜尋用的。
@property (strong, nonatomic) NSArray *arOriginal;
@property (strong, nonatomic) NSArray *arFiltered;
@property (readwrite, nonatomic) BOOL isSearching;
@property (weak, nonatomic) IBOutletUISearchBar *mySearchB;
self.isSearching = NO;
self.arOriginal = [NSArrayarrayWithObjects:@"Section0_Row1", @"Row1_Subrow1", @"Row1_Subrow2", @"Row1_Subrow3",nil];
self.mySearchB.delegate = self;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// return [self.contents count];
return (self.isSearching)?1:self.contents.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (self.isSearching)?self.arFiltered.count:[self.contents[section] count];
}
- (NSInteger)tableView:(SKSTableView *)tableView numberOfSubRowsAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isSearching) {
return 0;
}
return [self.contents[indexPath.section][indexPath.row] count] - 1;
}
///這個都沒有執行。。。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isSearching) {///搜尋到的結果。。。
static NSString *CellIdentifier = @"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
cell.textLabel.text = [self.arFiltered objectAtIndex:indexPath.row];
UIImageView *tempImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"DT_look.png"]];
tempImageView.frame = CGRectMake(280, 18, 20, 16);
[cell.contentView addSubview:tempImageView];
return cell;
}
static NSString *CellIdentifier = @"SKSTableViewCell";
SKSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[SKSTableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
cell.textLabel.text = self.contents[indexPath.section][indexPath.row][0];
NSLog(@"cell.textLabel.text==--------=%@",cell.textLabel.text);
///現在沒有section 并且都要有下拉的。。。。是以不用判斷。
cell.isExpandable = YES;
return cell;
}
#pragma mark =====真正執行的方法========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:@"%@", self.contents[indexPath.section][indexPath.row][indexPath.subRow]];
NSLog(@"cell.textLabel.text===%@",cell.textLabel.text);
cell.detailTextLabel.text = cell.textLabel.text;
UIImageView *tempImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"DT_look.png"]];
tempImageView.frame = CGRectMake(280, 18, 20, 16);
[cell.contentView addSubview:tempImageView];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SKSTableViewCell *myCell = (SKSTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"mycelll====%@",myCell.textLabel.text);
self.isSearching = NO;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
if(self.searchDisplayController.searchBar.text.length>0) {
self.isSearching=YES;
NSString *strSearchText = self.searchDisplayController.searchBar.text;
// NSMutableArray *ar=[NSMutableArray array];
// correctly working ! Thanx for watching video !
// for (NSString *d in self.arOriginal) {
// if([d rangeOfString:strSearchText].length>0) {
// [ar addObject:d];
// }
// }
// self.arFiltered=[NSArray arrayWithArray:ar];
///下面是用謂詞邏輯的方法。。///[c]忽略大小寫.[d]忽略重音。這兩個可以一起寫。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@", strSearchText];
self.arFiltered = [self.arOriginal filteredArrayUsingPredicate:predicate];
} else {
self.isSearching=NO;
}
returnYES;
}
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
///每次搜尋完的時候。
self.isSearching = NO;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end