ios - Swift Using NSFetchedResultsController to update TableView -
i have nsfetchedresultscontroller
populating tableview
. tableview
using custom cells.
when search getting correct results tableview isn't getting updated, ideas?
func controller( controller: nsfetchedresultscontroller, didchangeobject anobject: anyobject, atindexpath indexpath: nsindexpath?, forchangetype type: nsfetchedresultschangetype, newindexpath: nsindexpath?) { switch type { case nsfetchedresultschangetype.insert: if let insertindexpath = newindexpath { self.tbljobs.insertrowsatindexpaths([insertindexpath], withrowanimation: uitableviewrowanimation.fade) } case nsfetchedresultschangetype.delete: if let deleteindexpath = indexpath { self.tbljobs.deleterowsatindexpaths([deleteindexpath], withrowanimation: uitableviewrowanimation.fade) } case nsfetchedresultschangetype.update: // note update, update row @ __indexpath__ if let updateindexpath = indexpath { let cell = self.tbljobs.dequeuereusablecellwithidentifier( "jobcell") as! jobtableviewcell //let cell = self.tbljobs.cellforrowatindexpath(updateindexpath) let workitem = self.fetchedresultscontroller.objectatindexpath(updateindexpath) as? work }
and heres search function:
func searchbar(searchbar: uisearchbar, textdidchange searchtext: string) { println("search text \(searchtext)") savedsearchterm = searchtext if(savedsearchterm!.length == 0){ searchactive = false; } else { searchactive = true; self.fetchedresultscontroller.fetchrequest.predicate = nspredicate(format:"town contains[cd] %@", savedsearchterm!) var error: nserror? = nil self.fetchedresultscontroller.performfetch(&error) if (error != nil) { nslog("unable perform fetch.") nslog("%@, %@", error!, error!.localizeddescription) }else{ var filteredworkitems = self.fetchedresultscontroller.fetchedobjects println("fetched objects are: \(filteredworkitems)") } self.tbljobs.reloaddata() } }
update, cellforrowatindexpath function
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell{ println("getting hit") let cell = self.tbljobs.dequeuereusablecellwithidentifier( "jobcell") as! jobtableviewcell let workitem = fetchedresultscontroller.objectatindexpath(indexpath) as! work var parties: nsset = workitem.parties var arr = parties.allobjects //swift array var party = arr.first as! party var partyname = "\(party.title.desc) \(party.firstname) \(party.lastname)" cell.lbladdress?.text = "\(workitem.propertynumber) \(workitem.street) \(workitem.town) \(workitem.locality) \(workitem.postcode)" cell.lblschemetype?.text = workitem.scheme.desc let dateformatter = nsdateformatter() dateformatter.dateformat = "dd/mm/yy" let datestring = dateformatter.stringfromdate(workitem.createddate) cell.lblcreateddate?.text = datestring cell.lblpartydetails?.text = partyname return cell }
you can use same table searching , displaying. need disable superimposed table view (the uisearchcontroller
interface).
you need not use uisearchcontroller
, uisearchbar
. use uisearchbardelegate
methods trigger updates of original table.
Comments
Post a Comment