swift - How would I limit the amount of items in a tableview and automatically load more? -
again real quick helped me out.
the last thing i'm dealing follows.
i have search functionality in application , looks through parse keyword , matches accordingly. works fine now, list gets bigger, i'm assuming load times might little much. instantly shows 200 text results , i'm wondering if it'll crash or lag when showing 2,000.
the following important parts of search class.
func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return searchresults.count } func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { return uitableviewautomaticdimension } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let mycell = tableview.dequeuereusablecellwithidentifier("mycell", forindexpath: indexpath) as! uitableviewcell //mycell.backgroundview = uiimageview(image: uiimage(named: "goodbg")) //mycell.contentview.backgroundcolor = uiimage(named: "nobg") if mycell.backgroundview == nil { mycell.backgroundview = uiimageview(frame: mycell.contentview.bounds) mycell.backgroundview?.contentmode = uiviewcontentmode.scaletofill mycell.backgroundview?.clipstobounds = true mycell.contentview.backgroundcolor = uicolor.clearcolor() } //mycell.layoutmargins = uiedgeinsetszero let backgroundimageview = mycell.backgroundview as! uiimageview backgroundimageview.image = uiimage(named: "nobg") mycell.textlabel?.text = searchresults[indexpath.row] mycell.textlabel?.font = uifont(name: "lato-bold", size: 17) mycell.textlabel?.textcolor = uicolor(red: 126.0/255, green: 126.0/255, blue: 126.0/255, alpha: 1) mycell.textlabel?.linebreakmode = nslinebreakmode.bywordwrapping mycell.textlabel?.numberoflines = 0 return mycell } func searchbarsearchbuttonclicked(searchbar: uisearchbar) { searchbar.resignfirstresponder() var textsearchquery = pfquery(classname: "red") textsearchquery.wherekey("text", containsstring: searchbar.text) var query = pfquery.orquerywithsubqueries([textsearchquery]) query.findobjectsinbackgroundwithblock { (results: [anyobject]?, error: nserror?) -> void in if error != nil { //var myalert = uialertcontroller(title: "alert", message: error?.localizeddescription, preferredstyle: uialertcontrollerstyle.self) //let okaction = uialertaction(title: "ok", style: uialertactionstyle.default, handler: nil) //myalert.addaction(okaction) //self.presentviewcontroller(myalert, animated: true, completion: nil) return } if let objects = results as? [pfobject] { self.searchresults.removeall(keepcapacity: false) object in objects { let searchtext = object.objectforkey("text") as! string self.searchresults.append(searchtext) } dispatch_async(dispatch_get_main_queue()) { self.mytable.reloaddata() self.searchbar.resignfirstresponder() } } } }
how make maybe 100 items loaded first , subsequent items loaded once user near bottom of page.
first, should able query in ranges. then, combine count of items in self.searchresults
tableview(_: , willdisplaycell, forrowatindexpath:)
. is, row gets self.searchresults.count
have cell show "loading ..." , meanwhile trigger query in next range.
roughly speaking, in willdisplaycell:
check
if notalreadyfetching && self.searchresults.count == indexpath.row { // fetch next range of 'searchresults' }
and in cellforrowatindexpath:
have last table cell kind of loader (until model gets updated , triggers callback):
if self.searchresults.count == indexpath.row { // return loader cell }
Comments
Post a Comment