I am trying to build a tableview from some data that I have but I have come across some appending behaviour that I don't understand.
To debug I am trying this , if I say :
var sectionHeaders:[String] = ["0","1"]
var items:[[String]] = [["zero","zero","zero"],["one","one","one"]]
and display this in my tableview, I see a header "0" with three rows of zero, another header "1" with three rows of "one". Which is to be expected. However if I try and build the same structure using append, I get strange results :
var sectionHeaders:[String] = []
var items:[[String]] = [[]]
var tempItems:[String] = []
sectionHeaders.append("0")
tempItems.append("zero")
tempItems.append("zero")
tempItems.append("zero")
items.append(tempItems)
tempItems = []
sectionHeaders.append("1")
tempItems.append("one")
tempItems.append("one")
tempItems.append("one")
items.append(tempItems)
With this I get a section header of "0" with zero rows (no lines), and a header of "1" with three rows of zeros.
The data seems to be offset somehow
Is there something wrong with the way I am appending ?
My data source delegates are very simple :
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionHeaders[section]
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sectionHeaders.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath)
let text = items[indexPath.section][indexPath.row]
cell.textLabel?.text = text
return cell
}
}