我正在尝试将UISearchBar添加到UITableView,但是没有调用TextDidBeginEditing方法。我想我错过了一些简单但我无法理解的东西。
标题文件:
@interface TableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
@property (nonatomic, strong) NSMutableArray *initialCities;
@property (nonatomic, strong) NSMutableArray *filteredCities;
@property (nonatomic, strong) UISearchBar *searchBar;
@property BOOL isFiltered;
@end
实施档案:
#import "TableViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize initialCities, filteredCities, isFiltered, searchBar;
- (void)loadView
{
[super loadView];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
searchBar.delegate = self;
searchBar.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.tableView.tableHeaderView = searchBar;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self;
initialCities = [[NSMutableArray alloc] initWithObjects:@"London", @"New York", @"Berlin", nil];
}
#pragma mark - UITableView DataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isFiltered == YES)
{
return filteredCities.count;
}
else
{
return initialCities.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (isFiltered == YES)
{
cell.textLabel.text = [filteredCities objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [initialCities objectAtIndex:indexPath.row];
}
return cell;
}
#pragma mark - UITableView Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark - UISearchBar Delegate methods
- (void)searchBar:(UISearchBar *)searchBar TextDidBeginEditing:(NSString *)searchText
{
searchBar.showsCancelButton = YES;
if (searchText.length == 0)
{
isFiltered = NO;
}
else
{
isFiltered = YES;
filteredCities = [[NSMutableArray alloc] init];
for (NSString *cityName in initialCities)
{
NSRange cityNameRange = [cityName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (cityNameRange.location != NSNotFound)
{
[filteredCities addObject:cityName];
}
}
}
[self.tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
结果是显示搜索栏,显示表格视图,但是当我在搜索栏中键入文本时,表格视图不会被过滤。没有崩溃。任何帮助表示赞赏。谢谢。
答案 0 :(得分:1)
试试这个
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
// Your filter code
}
您实施的方法甚至不存在于协议
中– searchBar:textDidChange:
– searchBar:shouldChangeTextInRange:replacementText:
– searchBarShouldBeginEditing:
– searchBarTextDidBeginEditing:
– searchBarShouldEndEditing:
– searchBarTextDidEndEditing:
答案 1 :(得分:0)
此方法在searchBar的委托中不存在。
使用searchBarTextDidBeginEditing。
答案 2 :(得分:0)
完成此tutorial。很好解释。 http://www.appcoda.com/search-bar-tutorial-ios7/
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
searchResults = [actualData filteredArrayUsingPredicate:resultPredicate];
}