ios - tableview not refreshing by using notificationcentre and tableview.reloaddata() -
here code use refresh tableview when click delete button on custom tableview cell using notificationcenter , tableview.reloaddata(). have searched bunch of other codes , think code looks fine. don't know why doesn't refresh.
this tableview
override func viewdidload() { super.viewdidload() username = tempuser.username self.resultsearchcontroller = ({ let controller = uisearchcontroller(searchresultscontroller: nil) controller.searchresultsupdater = self controller.dimsbackgroundduringpresentation = false controller.searchbar.sizetofit() self.tableview.tableheaderview = controller.searchbar return controller })() get{(value) in self.values = value ele in self.values{ if self.username != ele["username"] as! string{ } } } self.tableview.reloaddata() nsnotificationcenter.defaultcenter().addobserver(self, selector: #selector(serviceboard.methodhandlingthenotificationevent), name:"notificationidentifier", object: nil) } func methodhandlingthenotificationevent(){ tableview.reloaddata() } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { if (self.resultsearchcontroller.active && resultsearchcontroller.searchbar.text != "") { return self.filteredtabledata.count } else { return values.count } } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as!postcell let maindata = values[values.count-1-indexpath.row] if (self.resultsearchcontroller.active && resultsearchcontroller.searchbar.text != "") { // if (filteredtabledata[indexpath.row].rangeofstring("###") == nil){ cell.postimg.image = uiimage(named:"tile_services") cell.title.text = filteredtabledata[indexpath.row] cell.category.text = "services" var price = string() in values{ if (i["title"] as? string)! == filteredtabledata[indexpath.row]{ price = (i["price"] as? string)! cell.categories = "services" cell.username = i["username"] as! string cell.prices = i["price"] as! string cell.notes = i["notes"] as! string cell.school = i["school"] as! string } } cell.price.text = price return cell } else { if maindata["username"] as! string != username && username != "admin"{ cell.deletebtn.hidden = true } else{ cell.categories = "services" cell.username = maindata["username"] as! string cell.prices = maindata["price"] as! string cell.notes = maindata["notes"] as! string cell.school = maindata["school"] as! string } cell.postimg.image = uiimage(named:"tile_services") cell.title.text = maindata["title"] as? string cell.category.text = "services" cell.price.text = maindata["price"] as? string return cell } } func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { let popovervc = uistoryboard(name: "main", bundle: nil).instantiateviewcontrollerwithidentifier("showpostt") as! showpostt self.addchildviewcontroller(popovervc) popovervc.view.frame = self.view.frame self.view.addsubview(popovervc.view) popovervc.didmovetoparentviewcontroller(self) if (self.resultsearchcontroller.active && resultsearchcontroller.searchbar.text != "") { in values{ if (i["title"] as? string)! == filteredtabledata[indexpath.row]{ popovervc.type.text = "service" popovervc.typeimg.image = uiimage(named:"tile_services") popovervc.item.text = "service: \(i["title"] as! string)" popovervc.price.text = "price: \(i["price"] as! string)" popovervc.notes.text = "notes: \(i["notes"] as! string)" popovervc.comments.text = i["comments"] as? string popovervc.postuser.text = i["username"] as! string popovervc.notesstr = i["notes"] as! string popovervc.category = "service" popovervc.pricesstr = i["price"] as! string if username == popovervc.postuser.text!{ popovervc.composebtn.hidden = true } } } } else{ let maindata = values[values.count-1-indexpath.row] popovervc.type.text = "service" popovervc.typeimg.image = uiimage(named:"tile_services") popovervc.item.text = "service: \(maindata["title"] as! string)" popovervc.price.text = "price: \(maindata["price"] as! string)" popovervc.notes.text = "notes: \(maindata["notes"] as! string)" popovervc.comments.text = maindata["comments"] as? string popovervc.postuser.text = maindata["username"] as! string popovervc.notesstr = maindata["notes"] as! string popovervc.category = "service" popovervc.pricesstr = maindata["price"] as! string if username == popovervc.postuser.text!{ popovervc.composebtn.hidden = true } } }
and custom tableview cell
class postcell: uitableviewcell{ @iboutlet weak var deletebtn: uibutton! @iboutlet weak var postimg: uiimageview! @iboutlet weak var category: uilabel! @iboutlet weak var location: uilabel! @iboutlet weak var title: uilabel! @iboutlet weak var price: uilabel! var prices = string() var notes = string() var comments = string() var locations = string() var categories = string() var school = string() var username = string() var date = string() @ibaction func deleteaction(sender: anyobject) { let request = nsmutableurlrequest(url: nsurl(string: "http://www.percyteng.com/orbit/deletepost.php")!) request.httpmethod = "post" let poststring = "name=\(username)&location=\(locations)&price=\(prices)¬es=\(notes)&school=\(school)&category=\(categories)" request.httpbody = poststring.datausingencoding(nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in if error != nil { print("error=\(error)") return } print("response = \(response)") let responsestring = nsstring(data: data!, encoding: nsutf8stringencoding) print("responsestring = \(responsestring)") } task.resume(); nsnotificationcenter.defaultcenter().postnotificationname("notificationidentifier", object: nil) } override func awakefromnib() { super.awakefromnib() } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated:animated) } }
it looks not updating values
property when click on "delete" button.
func methodhandlingthenotificationevent(){ // have update `values` array doesn't contain value you've removed. tableview.reloaddata() }
Comments
Post a Comment