ios - UITableView - different rows in sections with same array -


i have array values backend, , have uitableview 3 sections , different rows :

 override func numberofsectionsintableview(tableview: uitableview) -> int {     // #warning potentially incomplete method implementation.     // return number of sections.     return 3 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     // #warning incomplete method implementation.     // return number of rows in section.      if section == 1 {          return 2     }      else if section == 2 {          return 1      } else {          return 3     }  }     override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {       let mycell: celltableviewcell = tableview.dequeuereusablecellwithidentifier("mycell", forindexpath: indexpath) as! celltableviewcell      // configure cell...      if indexpath.section == 1 {          mycell.titlelabel.text = titles[indexpath.row + 3]      }     else if indexpath.section == 2 {          mycell.textlabel?.text = "section 2"      }     else {           mycell.titlelabel.text = titles[indexpath.row] // error!      }      return mycell }   override func tableview(tableview: uitableview, titleforheaderinsection section: int) -> string? {      return "section" } 

i need have cells 2 different sections data same array, , have section in middle should it's data different array. when run error:

fatal error: array index out of range 

my array contains 5 values, goes this:

 titles = ["1", "2", "3", "4", "5"] 

ps: have custom cells tableview

edit: edited sections arrangements ( first time knew indexed bottom top), still , error last section cells, there weird behaviour when scroll down up, section 2 replaces 3rd cell of section 1, , gets moved bottom of table !

you can't use loop you're doing since gives each cell in section value in last index loop through (index 2, or 4). normally, sectioned table view, use array of arrays populate sections. if want use single array, , number of rows in each section stay show them, following should work,

class viewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate {      var titles = ["1", "2", "3", "4", "5"]      override func viewdidload() {         super.viewdidload()     }       func numberofsectionsintableview(tableview: uitableview) -> int {         return 3     }      func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {          if section == 0 {              return 3         }         else if section == 1 {              return 1          } else {              return 2         }     }      func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {          let mycell: uitableviewcell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell          if indexpath.section == 0 {             mycell.textlabel!.text = titles[indexpath.row]         }         else if indexpath.section == 1 {             mycell.textlabel!.text = "section 2"         }         else {             mycell.textlabel!.text = titles[indexpath.row + 3]         }          return mycell     }  } 

this gives 1, 2, 3, "section 2", 4, 5 6 cells (i used standard uitableviewcell test case).


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -