How do I generate the same random color in a table view?

I have my table view setup so when I click on the row the table view expands into another row underneath the clicked row. I want to generate the same random color for the background for both rows when clicked. When I click the row, it generates 2 different random colors. This is my code.


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let dataIndex = indexPath.row - 1
        guard let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1") else {return UITableViewCell()}
        
        func generateRandomColor() -> UIColor {
            let redValue = CGFloat(drand48())
            let greenValue = CGFloat(drand48())
            let blueValue = CGFloat(drand48())
            
            let randomColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
            
            return randomColor
        }
        
        if indexPath.row == 0 {
            if tableViewData[indexPath.section].opened == false {
                tableViewData[indexPath.section].opened = false
                cell1.textLabel?.text = tableViewData[indexPath.section].title
                cell1.textLabel?.numberOfLines = 0
                cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
                cell1.textLabel?.font = UIFont.systemFont(ofSize: 18)
                cell1.backgroundColor = UIColor.clear
                return cell1
            }
            else if tableViewData[indexPath.section].opened == true {
                tableViewData[indexPath.section].opened = true
                cell1.textLabel?.text = tableViewData[indexPath.section].title
                cell1.textLabel?.numberOfLines = 0
                cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
                cell1.textLabel?.font = UIFont.boldSystemFont(ofSize: 25)
                cell1.backgroundColor = generateRandomColor()
                return cell1
            }
            return cell1
        } else {
            cell1.textLabel?.text = tableViewData[indexPath.section].sectionData[dataIndex]
            cell1.textLabel?.numberOfLines = 0
            cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
            cell1.textLabel?.font = UIFont.systemFont(ofSize: 18)
            cell1.backgroundColor = generateRandomColor()
            return cell1
        }
    }

This is an awkward problem to solve. What you'd really hope to do is to give the "expansion" row the same color as the immediately preceding row, but (depending on how the table is scrolled) the cell for the preceding row may not have been created.


The simplest solution is to generate and/or store the random number for each row in your tableViewData array. When you execute the tableView(_:cellForRowAt:) method for either row, if the color hasn't been determined yet, generate a random color and store it in the entries for both rows.

Try caching the random color for one row, then do an if>1 row count and use it for the second/additional row, cancelling a followon random color.

How do I generate the same random color in a table view?
 
 
Q