ios - UISearchBar issues Xcode 5 -
i having issues uisearchbar
have implemented. app compile , run, hit search bar begin typing word in, crashes. can tell me if see wrong syntax? suppose go through states have in 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 resources can 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; }
i new objective-c, sorry if simple question should know answer to.
thanks
the first line of - (void)searchthroughdata
method sets nsmutablearray nil. may problem.
Comments
Post a Comment