UISearchBar发行Xcode 5

javaGeek

我遇到了自己UISearchBar实施的问题。我的应用程序将编译并运行,但是当我点击搜索栏开始输入单词时,它就会崩溃。谁能告诉我您的语法有问题吗?它应该经历我自己的状态NSArray

TableViewController.h

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController <UISearchBarDelegate>

@property (nonatomic, strong) NSMutableArray *results;

@property (nonatomic, strong) IBOutlet UISearchBar *searchBar;

@end

TableViewController.m

#import "TableViewController.h"
#import "ViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
{
NSArray *states;
}

- (NSMutableArray *)results
{
if (!_results)
{
    _results = [[NSMutableArray alloc] init];
}
return _results;
}

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

- (void)viewDidLoad
{
[super viewDidLoad];

states = [NSArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", @"Colorado", nil];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)searchThroughData
{
self.results = nil;

NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] $@", self.searchBar.text];

self.results = [[states filteredArrayUsingPredicate:resultsPredicate] mutableCopy];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.tableView)
{
    return [states count];
}

else
{
    [self searchThroughData];
    return self.results.count;
}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

if(tableView == self.tableView)
{
    cell.textLabel.text = [states objectAtIndex:indexPath.row];
}

else
{
    cell.textLabel.text = self.results[indexPath.row];
}

return cell;
}

我对Objective-C还是很陌生,如果这是一个我应该知道答案的简单问题,对不起。

谢谢

用户名

方法的第一行将- (void)searchThroughDataNSMutableArray设置为nil。这可能是问题所在。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章