ios - Swift- How do I choose what cells go into what section (UITableView)? -


i have made this:

var data = ["apple", "apricot", "banana", "blueberry", "cantaloupe", "cherry",     "clementine", "coconut", "cranberry", "fig", "grape", "grapefruit",     "kiwi fruit", "lemon", "lime", "lychee", "mandarine", "mango",     "melon", "nectarine", "olive", "orange", "papaya", "peach",     "pear", "pineapple", "raspberry", "strawberry"] var months = ["january","february","march","april","may","june","july","august","september","october","november","december"]  override func numberofsectionsintableview(tableview: uitableview) -> int {     // #warning potentially incomplete method implementation.     // return number of sections.     return 12 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     // #warning incomplete method implementation.     // return number of rows in section.     return data.count }   override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("celllabel", forindexpath: indexpath) as! uitableviewcell      // configure cell...      cell.textlabel?.text = data[indexpath.row]      return cell }  override func tableview(tableview: uitableview, titleforheaderinsection section: int) -> string? {     return months[section] } 

now how choose variables in data array go "month" section? there method should use i'm unaware of? (this in class extends uitableview)

func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int , tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell have section , indexpath arguments, indexpath has section variable. should switch statements in these functions return desired data based on month. might better idea have array of data each month. or multidimensional array, or custom datatype. anyways, code might like:

override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     if section == 0 {         return datajanuary.count     }     else if section == 1 {         return datafebruary.count     }     // ...     return 0 }  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("celllabel", forindexpath: indexpath) as! uitableviewcell      // configure cell...     if indexpath.section == 0 {         cell.textlabel?.text = datajanuary[indexpath.row]     }     else if indexpath.section == 1 {         cell.textlabel?.text = datafebruary[indexpath.row]     }     // ...     return cell } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -