gpt4 book ai didi

ios - UISearchBar 没有过滤 UITableView

转载 作者:行者123 更新时间:2023-11-29 03:03:31 25 4
gpt4 key购买 nike

我正在尝试将 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

结果是显示搜索栏,显示表格 View ,但是当我在搜索栏中键入文本时,表格 View 未被过滤。没有崩溃。任何帮助表示赞赏。谢谢。

最佳答案

试试这个

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
// Your filter code
}

您实现的方法甚至不存在于协议(protocol)中

– searchBar:textDidChange:
– searchBar:shouldChangeTextInRange:replacementText:
– searchBarShouldBeginEditing:
– searchBarTextDidBeginEditing:
– searchBarShouldEndEditing:
– searchBarTextDidEndEditing:

关于ios - UISearchBar 没有过滤 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23030168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com