I have an array which I get its values from a backend, and I have a UITableView with 3 sections and different rows :
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the 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 the 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 to have the cells from 2 different sections to get their data from the same array, and I have a section in the middle which should get it's data from a different array. When I run I get this error:
fatal error: Array index out of range
my array contains 5 values, it goes like this:
titles = ["1", "2", "3", "4", "5"]
PS: I have custom cells for my TableView
Edit: I edited the sections arrangements ( first time I knew that they are indexed from bottom to top), but I still get and error with my last section cells, also there is a very weird behaviour where when I scroll down then back up, section 2 replaces the 3rd cell of section 1, and it gets moved to the bottom of the table !