Monday, 15 September 2014

ios - Reload detail view when table cell is deleted - Swift -



ios - Reload detail view when table cell is deleted - Swift -

i have split view controller (master-detail). i'm optimizing iphone 6 plus.

here's issue. when select cell, performs show-detail segue , displays cells info in detailed view. however, when delete cell, detailed view keeps cells info though cell no longer exists.

here's example:

func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { if (editingstyle == uitableviewcelleditingstyle.delete) { //code deletes cell/not needed illustration tableview.deleterowsatindexpaths([indexpath], withrowanimation: .left) allow splitcontroller = self.splitviewcontroller!.viewcontrollers self.detailviewcontroller = splitcontroller.last? as? detailedcontroller if allow detail = detailviewcontroller { detail.title = "select item" //detail.title variable in view controller //on views "viewdidload" sets label.text title. } } }

i want perform action: detail.title = "select item", view no longer displaying deleted cell. tried making new segue through code. no luck.

ideas?

do following:

make sure segue sets detail view controller has identifier. in sample master-detail project identifier "showdetail". can find identifier selecting segue in document outline view of project, , looking in identifier field in attributes inspector segue.

call segue programmatically when delete row:

override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { if editingstyle == .delete { // phone call showdetail when in splitviewcontroller , 2 view controllers // nowadays if self.splitviewcontroller?.viewcontrollers.count == 2 { self.performseguewithidentifier("showdetail", sender: self) } ...

in prepareforsegue create sure check selected row. when phone call segue cell deletion code no row selected , indexpathforselectedrow() homecoming nil:

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "showdetail" { if allow indexpath = self.tableview.indexpathforselectedrow() { // set destination view controller in normal way ... } else { // no row selected means either initial setup // or came here row delete code allow splitcontroller = self.splitviewcontroller!.viewcontrollers self.detailviewcontroller = splitcontroller.last? as? detailedcontroller if allow detail = detailviewcontroller { detail.title = "select item" ... } } }

note: if detail view controller has default state when detail.title not set, , default state want destination view controller if no row has been selected, don't need else clause in prepareforsegue. case sample master-detail ios app. necessary phone call segue programmatically edit code, , works.

ios uitableview swift

No comments:

Post a Comment