\n
and the incorrect display after scrolling about :
\n\nmy cellForRowAt code :
\n override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n \n let cell = tableView.dequeueReusableCell(withIdentifier: "historyGoalCell", for: indexPath)\n \n let name = items[indexPath.section][indexPath.row].name\n let date = dateManager.dateAsString(for: items[indexPath.section][indexPath.row].date!)\n \n \n if tempDate != date {\n \n // show header\n \n cell.textLabel?.text = date\n \n tempDate = date\n \n } else {\n \n // don't show header\n \n cell.textLabel?.text = ""\n \n }\n \n cell.detailTextLabel?.text = "\\(date),\\(name ?? "")"\n \n return cell\n }\n
\nThanks for any help, I have been stuck with this for a couple of days, very new to TableViews - thanks
\n","author":{"@type":"Person","name":"jat"},"upvoteCount":0,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell\n
\ncan be invoked in any order. It isn't called consistently from 1 to N, so logic with tempDate
works not as planned. Better do some pre-work and make an array with indexes where you place headers. For example
struct Pair : Hashable {\n var i : Int\n var j : Int\n}\n\n//Somewhere one time before the first reloadData\n\nvar hasIndex : Set<Pair> = []\nvar tempDate: Date = Date.distantPast\n\nfor i in 0..<sections {\n for j in 0..<rows[i] {\n let name = items[i][j].name\n let date = dateManager.dateAsString(for: items[i][j].date!)\n if tempDate != date {\n hasIndex.insert(Pair(i: i, j: j))\n // OR items[i][j].showHeader = true\n tempDate = date\n } else {\n // OR items[i][j].showHeader = false\n }\n }\n}\n\n...\n\noverride func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n\n let cell = tableView.dequeueReusableCell(withIdentifier: "historyGoalCell", for: indexPath)\n let name = items[indexPath.section][indexPath.row].name\n let date = dateManager.dateAsString(for: items[indexPath.section][indexPath.row].date!)\n\n if hasIndex.contains(Pair(i: indexPath.section, j: indexPath.row)) {\n // OR if items[indexPath.section][indexPath.row].showHeader {\n cell.textLabel?.text = date\n tempDate = date\n } else {\n cell.textLabel?.text = ""\n }\n\n cell.detailTextLabel?.text = "\\(date),\\(name ?? "")"\n\n return cell\n}\n
\n","author":{"@type":"Person","name":"AntiVIRUZ"},"upvoteCount":1}}}Reputation: 347
I am having an issue with what I think is reusable cells in a tableview cell, unfortunately I can't work out how to force the state of the cell. I am fairly sure this is the issue because when I reload the tableview everything is displayed correctly. It's only when I scroll that I start to see issues, if I once again reload the display corrects itself.
This is the correct display :
and the incorrect display after scrolling about :
my cellForRowAt code :
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "historyGoalCell", for: indexPath)
let name = items[indexPath.section][indexPath.row].name
let date = dateManager.dateAsString(for: items[indexPath.section][indexPath.row].date!)
if tempDate != date {
// show header
cell.textLabel?.text = date
tempDate = date
} else {
// don't show header
cell.textLabel?.text = ""
}
cell.detailTextLabel?.text = "\(date),\(name ?? "")"
return cell
}
Thanks for any help, I have been stuck with this for a couple of days, very new to TableViews - thanks
Upvotes: 0
Views: 580
Reputation: 342
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
can be invoked in any order. It isn't called consistently from 1 to N, so logic with tempDate
works not as planned. Better do some pre-work and make an array with indexes where you place headers. For example
struct Pair : Hashable {
var i : Int
var j : Int
}
//Somewhere one time before the first reloadData
var hasIndex : Set<Pair> = []
var tempDate: Date = Date.distantPast
for i in 0..<sections {
for j in 0..<rows[i] {
let name = items[i][j].name
let date = dateManager.dateAsString(for: items[i][j].date!)
if tempDate != date {
hasIndex.insert(Pair(i: i, j: j))
// OR items[i][j].showHeader = true
tempDate = date
} else {
// OR items[i][j].showHeader = false
}
}
}
...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "historyGoalCell", for: indexPath)
let name = items[indexPath.section][indexPath.row].name
let date = dateManager.dateAsString(for: items[indexPath.section][indexPath.row].date!)
if hasIndex.contains(Pair(i: indexPath.section, j: indexPath.row)) {
// OR if items[indexPath.section][indexPath.row].showHeader {
cell.textLabel?.text = date
tempDate = date
} else {
cell.textLabel?.text = ""
}
cell.detailTextLabel?.text = "\(date),\(name ?? "")"
return cell
}
Upvotes: 1