objective c - Add Search to tvOS UITableView -
i have tableview on 400 objects on tvos app, , needs have search functionality on it.
after reading through sample code apple, here have. secondviewcontroller main tableview is, while kept name of resultsviewcontroller same apple had it. in main 1 have:
-(void)viewdidload { [super viewdidload]; _resultstablecontroller = [[aplresultstablecontroller alloc] init]; _searchcontroller = [[uisearchcontroller alloc] initwithsearchresultscontroller:self.resultstablecontroller]; self.searchcontroller.searchresultsupdater = self; [self.searchcontroller.searchbar sizetofit]; self.tableview.tableheaderview = self.searchcontroller.searchbar; // want delegate our filtered table didselectrowatindexpath called both tables self.resultstablecontroller.tableview.delegate = self; self.searchcontroller.delegate = self; self.searchcontroller.searchbar.delegate = self; // can monitor text changes + others // search presenting view controller. such, normal view controller // presentation semantics apply. namely presentation walk view controller // hierarchy until finds root view controller or 1 defines presentation context. // self.definespresentationcontext = yes; // know want uisearchcontroller displayed } - (void)viewwillappear:(bool)animated { if (self.searchcontrollerwasactive) { self.searchcontroller.active = self.searchcontrollerwasactive; _searchcontrollerwasactive = no; if (self.searchcontrollersearchfieldwasfirstresponder) { [self.searchcontroller.searchbar becomefirstresponder]; _searchcontrollersearchfieldwasfirstresponder = no; } } nsbundle *bundle = [nsbundle mainbundle]; self.files = [bundle pathsforresourcesoftype:@"pdf" indirectory:@"aimpdf"]; nsstring *documentsdirectorypath = [self.files objectatindex:thepath.row]; self.title = @"devo songs"; self.filenames = [[documentsdirectorypath lastpathcomponent] stringbydeletingpathextension]; nslog(@"%@", filenames); if ([self savedsearchterm]) { //[[[self searchdisplaycontroller] searchbar] settext:[self savedsearchterm]]; } nsmutablearray *names = [nsmutablearray arraywithcapacity:[self.files count]]; (nsstring *path in self.files) { [names addobject:[[path lastpathcomponent] stringbydeletingpathextension]]; } self.files = names; self.tableview.backgroundcolor = [uicolor whitecolor]; self.parentviewcontroller.view.backgroundcolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"iphonebackground.png"]]; [super viewdidload]; uibarbuttonitem *plan = [[uibarbuttonitem alloc] initwithtitle:@"plan devo" style:uibarbuttonitemstyleplain target:self action:@selector(picking)]; self.navigationitem.rightbarbuttonitem = plan; [super viewwillappear:animated]; } - (void)searchbarsearchbuttonclicked:(uisearchbar *)searchbar { [searchbar resignfirstresponder]; } - (void)updatesearchresultsforsearchcontroller:(uisearchcontroller *)searchcontroller { // update filtered array based on search text nsstring *searchtext = searchcontroller.searchbar.text; nsmutablearray *searchresults = [self.files mutablecopy]; // strip out leading , trailing spaces nsstring *strippedstring = [searchtext stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]]; // break search terms (separated spaces) nsarray *searchitems = nil; if (strippedstring.length > 0) { searchitems = [strippedstring componentsseparatedbystring:@" "]; } // build "and" expressions each value in searchstring // // hand on filtered results our search results table aplresultstablecontroller *tablecontroller = (aplresultstablecontroller *)self.searchcontroller.searchresultscontroller; tablecontroller.filteredproducts = searchresults; [tablecontroller.tableview reloaddata]; } - (void)handlesearchforterm:(nsstring *)searchterm { [self setsavedsearchterm:searchterm]; if ([self searchresults] == nil) { nsmutablearray *array = [[nsmutablearray alloc] init]; [self setsearchresults:array]; array = nil; } [[self searchresults] removeallobjects]; if ([[self savedsearchterm] length] != 0) { (nsstring *currentstring in [self files]) { if ([currentstring rangeofstring:searchterm options:nscaseinsensitivesearch].location != nsnotfound) { [[self searchresults] addobject:currentstring]; } } } } in results view controller, have:
@implementation aplresultstablecontroller - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return self.filteredproducts.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *filename = [[[self.filteredproducts objectatindex:indexpath.row] lastpathcomponent] stringbydeletingpathextension]; nsinteger row = [indexpath row]; nsstring *contentforthisrow = nil; contentforthisrow = filename; static nsstring *cellidentifier = @"cellidentifier"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } [[cell textlabel] settext:contentforthisrow]; cell.textlabel.font = [uifont fontwithname:@"helvetica neue" size:90]; cell.textlabel.textcolor = [uicolor blackcolor]; cell.backgroundcolor = [uicolor lightgraycolor]; return cell; } @end i search bar added @ top of main tableview, nothing changes when start typing in letters.
apple deprecated uisearchdisplaycontroller in ios 8, , removed ios 9, why it's not available on tvos.
you need use uisearchcontroller, initialize , present in code. apple provides example of how in searchviewcontroller.swift, in uikitcatalog tvos sample code.
Comments
Post a Comment