ios - SearchBar only returning first tableview cell -
i have app in xcode core data has searchbar. searchbar searches through title , message of notes , displays cell containing keyletter entered. however, searchbar showing first result when type in anything. thanks
#import "deviceviewcontroller.h" #import "devicedetailviewcontroller.h" @interface deviceviewcontroller () @property (strong) nsmutablearray *devices; @end @implementation deviceviewcontroller { nsarray *searchresults; } - (nsmanagedobjectcontext *)managedobjectcontext { nsmanagedobjectcontext *context = nil; id delegate = [[uiapplication sharedapplication] delegate]; if ([delegate performselector:@selector(managedobjectcontext)]) { context = [delegate managedobjectcontext]; } return context; } - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { } return self; } - (void)viewdidload { [super viewdidload]; uilabel *label = [[uilabel alloc] initwithframe:cgrectzero]; //label.backgroundcolor = [uicolor clearcolor]; label.font = [uifont fontwithname:@"helveticaneue-thin" size:28]; //label.shadowcolor = [uicolor colorwithwhite:0.0 alpha:0.5]; label.textcolor = [uicolor blackcolor]; self.navigationitem.titleview = label; label.text = @"tapnotes"; [label sizetofit]; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; // fetch devices persistent data store nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"device"]; self.devices = [[managedobjectcontext executefetchrequest:fetchrequest error:nil] mutablecopy]; [self.tableview reloaddata]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (tableview == self.searchdisplaycontroller.searchresultstableview) { return [searchresults count]; } else { return self.devices.count; } //return self.devices.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [self.tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell==nil) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } nsmanagedobject *device = [self.devices objectatindex:indexpath.row]; [cell.textlabel settext:[nsstring stringwithformat:@"%@", [device valueforkey:@"name"]]]; [cell.detailtextlabel settext:[nsstring stringwithformat:@"%@",[device valueforkey:@"version"]]]; if (tableview == self.searchdisplaycontroller.searchresultstableview) { device = [searchresults objectatindex:indexpath.row]; } else { [self.devices objectatindex:indexpath.row]; } return cell; } - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { nsmanagedobjectcontext *context = [self managedobjectcontext]; if (editingstyle == uitableviewcelleditingstyledelete) { // delete object database [context deleteobject:[self.devices objectatindex:indexpath.row]]; nserror *error = nil; if (![context save:&error]) { nslog(@"can't delete! %@ %@", error, [error localizeddescription]); return; } // remove device table view [self.devices removeobjectatindex:indexpath.row]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } } /* // override support editing table view. - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { // delete row data source [tableview deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; } else if (editingstyle == uitableviewcelleditingstyleinsert) { // create new instance of appropriate class, insert array, , add new row table view } } */ /* // override support rearranging table view. - (void)tableview:(uitableview *)tableview moverowatindexpath:(nsindexpath *)fromindexpath toindexpath:(nsindexpath *)toindexpath { } */ /* // override support conditional rearranging of table view. - (bool)tableview:(uitableview *)tableview canmoverowatindexpath:(nsindexpath *)indexpath { // return no if not want item re-orderable. return yes; } */ - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([[segue identifier] isequaltostring:@"updatedevice"]) { nsmanagedobject *selecteddevice = [self.devices objectatindex:[[self.tableview indexpathforselectedrow] row]]; nsindexpath *indexpath = nil; //device *device = nil; if (self.searchdisplaycontroller.active) { indexpath = [self.searchdisplaycontroller.searchresultstableview indexpathforselectedrow]; _devices = [searchresults objectatindex:indexpath.row]; } else { indexpath = [self.tableview indexpathforselectedrow]; _devices = [_devices objectatindex:indexpath.row]; } devicedetailviewcontroller *destviewcontroller = segue.destinationviewcontroller; destviewcontroller.device = selecteddevice; } } - (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope { nspredicate *resultpredicate = [nspredicate predicatewithformat:@"name contains[c] %@", searchtext]; searchresults = [_devices filteredarrayusingpredicate:resultpredicate]; } -(bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchstring:(nsstring *)searchstring { [self filtercontentforsearchtext:searchstring scope:[[self.searchdisplaycontroller.searchbar scopebuttontitles] objectatindex:[self.searchdisplaycontroller.searchbar selectedscopebuttonindex]]]; return yes; } @end
Comments
Post a Comment