【问题标题】:UISearchBar is not filtering UITableViewUISearchBar 没有过滤 UITableView
【发布时间】:2014-05-26 15:31:05
【问题描述】:

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

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

【问题讨论】:

  • 希望您使用的是 ARC...

标签: ios uitableview uisearchbar


【解决方案1】:

试试这个

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

您实现的方法甚至没有出现在协议中

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

【讨论】:

  • 是的,这行得通。我最初有 textDidChange ,但当时我猜由于其他一些原因它没有工作。我应该再试一次。非常感谢!!
【解决方案2】:
  • (void)searchBar:(UISearchBar *)searchBar TextDidBeginEditing:(NSString *)searchText

searchBar 的委托中不存在此方法。

使用 searchBarTextDidBeginEditing。

【讨论】:

    【解决方案3】:

    通过这个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];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-06
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      相关资源
      最近更新 更多