How do I get the text of a row in a table view to bold when it expands?

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
            
        }
    }
Answered by Claude31 in 351520022

So the cause was:

You set to bold line 19, but I see nowhere you reset to normal font.


Thanks for feedback.


Don't forget to close the thread on the correct answer.

Not sure to understand exactly what you have.


I have a table view with two prototype cells.

Are they prototypes for different sections ? Or for header and for cell ? For something else, just row 0 and other rows (looks like this in code) ?

Note: it is good to select identifiers that are meaningful (cell1 and cell2 do not tell a lot)


When I click on a row it expands just like I want it to and I got the part I click to Bold

Is it row 0 ? or other row ? or all rows ?

What is this expansion effect ?

If row > 0, the section is opened (line 48). And never closed. Is it on purpose ?


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.


You set to bold line 19, but I see nowhere you reset to normal font. Is it on purpose ?


Note: you could streamline your code a little:


   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     
        if indexPath.row == 0 {
              let sections = IndexSet.init(integer: indexPath.section)
              tableView.reloadSections(sections, with: .none)

              if !tableViewData[indexPath.section].opened  {
                tableView.deselectRow(at: indexPath, animated: true)
              }
              tableViewData[indexPath.section].opened = !tableViewData[indexPath.section].opened
        }
        else if indexPath.row > 0 {
            tableViewData[indexPath.section].opened = true
          
        }
    }

Thank you. I figured it out. Some of the rows that weren't expanded were bold because I never set the font to normal. Once I set the font to normal where In wanted it normal it worked good.

Accepted Answer

So the cause was:

You set to bold line 19, but I see nowhere you reset to normal font.


Thanks for feedback.


Don't forget to close the thread on the correct answer.

How do I get the text of a row in a table view to bold when it expands?
 
 
Q