I am creating an app with a table view. I have a table view with two prototype cells. When I click on a row it expands just like I want it to and I got the part I click to Bold but as I scroll down some of the rows that aren't expanded are already bold. I only want it to be bold when I click the row which is the title and I want it to display data when it expands into a new row which is the section data.
Here is my code for the two table view functions:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dataIndex = indexPath.row - 1
guard let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1") else {return UITableViewCell()}
guard let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2") else {return UITableViewCell()}
if indexPath.row == 0 {
if tableViewData[indexPath.section].opened == false {
tableViewData[indexPath.section].opened = false
cell1.textLabel?.numberOfLines = 0
cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell1.textLabel?.text = tableViewData[indexPath.section].title
return cell1
}
else if tableViewData[indexPath.section].opened == true {
tableViewData[indexPath.section].opened = true
guard let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1") else {return UITableViewCell()}
cell1.textLabel?.numberOfLines = 0
cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell1.textLabel?.text = tableViewData[indexPath.section].title
cell1.textLabel?.font = UIFont.boldSystemFont(ofSize: 20)
return cell1
}
return cell1
}
else {
cell2.textLabel?.numberOfLines = 0
cell2.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell2.textLabel?.text = tableViewData[indexPath.section].sectionData[dataIndex]
return cell2
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if tableViewData[indexPath.section].opened == true {
tableViewData[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
} else {
tableViewData[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
tableView.deselectRow(at: indexPath, animated: true)
}
}
else if indexPath.row > 0 {
tableViewData[indexPath.section].opened = true
}
}