ios - Swift NSUserDefault for TableView with Multiple Sections? -
i'm doing xcode project (swift) , have few problems relate nsuserdefault , prepareforsegue.
i have 2 tableviews. tableview1 has 2 sections: section1 , section2. here want:
- when click random row in section1 in tableview1, tableview2 shows number of sections want. (for example: click row 1 tableview2 shows 3 sections, click row 2 tableview2 shows 4 sections, number of sections show in tableview2 have different when click different rows)
same above section2 in tableview1
i use array of 2 arrays show 2 sections in tableview1.
sorry if question stupid, i'm new programming hope guys can show me how project. appreciate :)
background
in order make answer clearer, assuming have 2 view controllers, table1viewcontroller
, table2viewcontroller
, display table1
, table2
. assume know how wire 2 view controllers in interface builder can click on table1
, have table2viewcontroller
presented. describe in question, when user clicks on row in table1
determines how many sections there should in table2
. not discuss related mapping rows of table1
number of sections table2
.
the main issue how send chosen number of sections table2viewcontroller
.
you communicate information using nsuserdefaults
mention in question, not how user defaults intended used. documentation nsuserdefaults
says the defaults system allows application customize behavior match user’s preferences. , gives following examples: allow users determine units of measurement application displays or how documents automatically saved. instead designing application behave based on previous user selection.
so if aren't going use nsuserdefaults
, how might go this? mention uiviewcontroller
method prepareforseque:sender:
. documentation reads: [s]ubclasses override method , use configure new view controller prior being displayed.
main approach
and that's want do. before table2viewcontroller
displayed, want configure number of sections table2
should have. let's add property table2viewcontroller
follows (note wherever there "..." in following, leaving out of code):
class table2viewcontroller: ... { var numberofsections = 0 func numberofsectionsintableview(tableview: uitableview) -> int { return numberofsections } ... ... ... }
so remains after user clicks on row in table1
, determine how many sections table2
should have (i'm leaving part unspecified) , before table2viewcontroller
displayed need configure numberofsections
. in prepareforsegue
call. following should work:
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if let selectedindexpath = tableview.indexpathforselectedrow() { var destinationvc = segue.destinationviewcontroller as! table2viewcontroller destinationvc.nummberofsections = self.numberofsectionsfor(selectedindexpath) } }
so we've first determined index path selected row. cast segue.destinationviewcontroller
table2viewcontroller
can access properties. set numberofsections
property using method on table1viewcontroller
.
Comments
Post a Comment