I have a table view with ten entries, which refers to another view. If I click on one cell the color changes to light grey and shows the next page. I want to set this color to black when the table view cell is clicked. How can I do this?
Create a var to keep the selected row
var selectedIndex = IndexPath(row: -1, section: 0)
In didSelectRowAt:
In cellForRowAt
var selectedIndex = IndexPath(row: -1, section: 0)
In didSelectRowAt:
Code Block override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let indexesToRedraw = [indexPath] selectedIndex = indexPath // Do what you need here tableView.reloadRows(at: indexesToRedraw, with: .fade) // go to next "page" }
In cellForRowAt
Code Block override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Some identifier", for: indexPath) // Load the cell content if selectedIndex == indexPath { cell.backgroundColor = UIColor.black } return cell }