ios - Search in a tableView Cell issue -
i using xcode 5 , want search tableview data. used "search bar & search display controller" "searching", "customcell" "tablecell" , data parsing remote server nsxmlparsing. data showing corresponding image without problem how search option doesn't work properly. same code single array searsing , working. here isn't. @ time of searching crashed. here code:
-(void)loaddata { countryparser = [[countryparser alloc] loadxmlbyurl:@"http://www.avowstudio.com/ios/partproject/ios7/xmlparsingwithcustomecell/xmlfiles/xmlparsingwithcustomcell.xml"]; countryarray = [countryparser countryarray]; displayitems = [[nsmutablearray alloc] initwitharray:countryarray]; [self.countrytableview reloaddata]; [activityindicator stopanimating]; [self loadarray]; } -(void) loadarray { countryinfo *currentcountryone = [[countryinfo alloc] init]; totalarray = [[nsmutablearray alloc] init]; (int i=0; < [countryarray count]; i++) { currentcountryone = [countryarray objectatindex:i]; [totalarray addobject:currentcountryone.countryname]; } } -(void) searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext { if ([searchtext length] == 0) { [displayitems removeallobjects]; [displayitems addobjectsfromarray:countryarray]; } else { [displayitems removeallobjects]; displayitems = [[nsmutablearray alloc] init]; (nsstring *string in totalarray) { nsrange stringrang = [string rangeofstring:searchtext options:nscaseinsensitivesearch]; if (stringrang.location != nsnotfound) { [displayitems addobject:string]; } } } [self.countrytableview reloaddata]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [displayitems count]; } // method kept "tablerow height" after "done searching" - (void)searchdisplaycontroller:(uisearchdisplaycontroller *)controller didloadsearchresultstableview:(uitableview *)tableview { tableview.rowheight = 100; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; countrycustomcell *cell = [self.countrytableview dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) { cell = [[countrycustomcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } currentcountry = [displayitems objectatindex:indexpath.row]; cell.continentlabel.text = currentcountry.continent; cell.countrynamelabel.text = currentcountry.countryname; cell.capitallabel.text = currentcountry.capital; imagequeuecountryflag = dispatch_get_global_queue(dispatch_queue_priority_high, 0ul); dispatch_async(imagequeuecountryflag, ^ { uiimage *imagecountryflag = [uiimage imagewithdata:[nsdata datawithcontentsofurl: [nsurl urlwithstring:[currentcountry countryflag]]]]; dispatch_async(dispatch_get_main_queue(), ^ { cell.countryflagimageview.image = imagecountryflag; [cell setneedslayout]; }); }); return cell;
}
i don't want use 2 array in "numberofrowsinsection" & "cellforrowatindexpath" flag or thing that, avoid more coding. if 1 familiar please share here. lot of in advanced.
i think added objects of countryinfo in [countryparser countryarray]. in loadarray method, initializing blank object currentcountryone , each time loop executes, trying add blank object's property totalarray. please make changes below:
-(void) loadarray { totalarray = countryarray; } -(void) searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext { if ([searchtext length] == 0) { [displayitems removeallobjects]; [displayitems addobjectsfromarray:countryarray]; } else { [displayitems removeallobjects]; displayitems = [[nsmutablearray alloc] init]; (countryinfo *country in totalarray) { nsrange stringrang = [country.countryname rangeofstring:searchtext options:nscaseinsensitivesearch]; if (stringrang.location != nsnotfound) { [displayitems addobject:country]; } } } [self.countrytableview reloaddata]; }
Comments
Post a Comment