uitableview – 故事板Xcode中的UISearchBar

我想在storyboard xcode中为我的表创建searchBar,Everythings适合我,我没有任何错误,只是搜索不起作用.

我看到了这两个教程,但仍然无效.我无法搜索.
我的教程:

-https://stackoverflow.com/questions/9897171/uisearchbar-implemented-with-storyboards
   -http://www.youtube.com/watch?v=IqDZHgI_s24

你能帮帮我吗
提前致谢!

这是我的代码

CreateViewController.h

#import <UIKit/UIKit.h>

@interface CreateViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
{
// @property (weak,nonatomic) IBOutlet UISearchBar *searchBar;
NSArray *datas;
NSMutableArray *displayItems;
IBOutlet UITableView * tableView;
IBOutlet UISearchBar * searchbar;
}

@end

CreateViewController.m

#import "CreateViewController.h"

@interface CreateViewController ()

@end

@implementation CreateViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

datas = [NSMutableArray arrayWithObjects:@"Johan",@"Paul",@"George",@"Ringo",nil];
displayItems = [[NSMutableArray alloc] initWithArray:datas];
[super viewDidLoad];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [datas count];
}

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";//This is the identifier in storyboard    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

cell.textLabel.text = [datas objectAtIndex:indexPath.row];
return cell;
}
-(void)searchbar:(UISearchBar *)searchbar textDidChange:(NSString *)searchText{
if([searchText length] == 0){
    [displayItems removeAllObjects];
    [displayItems addObjectsFromArray:datas];
}else{
    [displayItems removeAllObjects];
    for(NSString * string in datas){
        NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (r.location != NSNotFound){
            [displayItems addObject:string];   
        }
    }
}
[tableView reloadData];
}

 #pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib    name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */
}

@end

解决方法

使用本教程iOS快速提示:使用搜索栏过滤UITableView

http://code-ninja.org/blog/2012/01/08/ios-quick-tip-filtering-a-uitableview-with-a-search-bar/

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = true;
    filteredTableData = [[NSMutableArray alloc] init];

    for (Food* food in allTableData)
    {
        NSRange nameRange = [food.name rangeOfString:text options:NSCaseInsensitiveSearch];
        NSRange descriptionRange = [food.description rangeOfString:text options:NSCaseInsensitiveSearch];
        if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
        {
            [filteredTableData addObject:food];
        }
    }
}

[self.tableView reloadData];
}

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...