tableview - What is the best way to pass data in a hierarchical structure? Swift -
so working on app has several tableviews take detailview , detailview can either take mapview or webview, here example of mean:
instead of making several detail groups (detail,web,map), make tableviews, take same detailview , put information there because there going lot of rows information in wouldn't possible. not of issue right now, think not doing things should. pass information way: in "prepareforsegue" funcion, tableview detailview use "if indexpath.row == 0", pass information according row selected, have integer variable set number of row clicked on tableview , too, passed detailview in detailview know website pass webview or location mapview, more spots added tableview, have add more "ifs" , not sure if right approach or there simpler way this.
you should have class encapsulates information relates single table row / detail view. let's call model
.
in table view controller have array of model
s, e.g.
var models = [model]()
the override cellforrowatindexpath
return cell based on particular model, e.g.
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("somecellid", forindexpath: indexpath) as! uitableviewcell let model = models[indexpath.row] // set title of cell title of logitem cell.textlabel?.text = model.name cell.detailtextlabel?.text = model.details return cell }
in storyboard make segue detail view, pass entire model
detail view
// in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. if (segue.identifier == "seguetodetail") { var svc = segue.destinationviewcontroller as! detailviewcontroller svc.model = models[tableview.indexpathforselectedrow()!.row] } }
this way passing 1 object , don't have have set detail view labels etc. detail view can pass same object onto map view or other view.
Comments
Post a Comment