问题部分解决,见帖子结尾:
UITableView
在不工作时显示搜索结果。table
保持空白,不显示任何内容results
,但searching
必须工作,因为当我search
处理不在其中的内容时,table
我会立即"No Results"
在table
.
注意:在我IB
用我connect
的delegate
.UISearchBar
Connection Inspector
Controller
希望你能帮忙。
米:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (tableView == self.searchDisplayController.searchResultsTableView) ? [self.seriesArrayFiltered count] : [self.seriesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SeriesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Serie *aSerie = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"search is displaying");
aSerie = self.seriesArrayFiltered[[indexPath row]];
} else {
NSLog(@"search is NOT displaying");
aSerie = self.seriesArray[[indexPath row]];
}
UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
valueLbl.text = aSerie.name;
return cell;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.seriesArrayFiltered removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
self.seriesArrayFiltered = [NSMutableArray arrayWithArray:[self.seriesArray filteredArrayUsingPredicate:predicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
H:
#import <UIKit/UIKit.h>
@interface SeriesTableVC : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
@property (nonatomic, strong) NSArray *seriesArray;
@property (nonatomic, strong) NSMutableArray *seriesArrayFiltered;
@property (weak, nonatomic) IBOutlet UISearchBar *seriesSearchBar;
@end
当我更改以下代码时,我现在可以看到搜索结果,但我想知道为什么无法识别标签。
在:cellForRowAtIndexPath
//UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
//valueLbl.text = aSerie.name;
cell.textLabel.text = aSerie.name;