as diu
as diu

Reputation: 1090

How to change separator color for UITableView only for first row?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0 {
        tableView.separatorColor = UIColor.clear
    } else {
        tableView.separatorColor = UIColor.blue
    }
}

I have the above code to change the separator color for only first row. However, the whole section itself turns out with a clear color separator instead of blue. Could you please help me with this. Any help would be greatly appreciated. Thank you.

Upvotes: 0

Views: 2739

Answers (1)

rishi
rishi

Reputation: 11839

Don't use tableview default seperator.

tableView.separatorColor = UIColor.clear or tableView.separatorStyle = .none

In UITableViewCell add a UIView of 1 px height, give that whatever color you want. If same UITableViewCell is used for all the table rows, then might be you can use something like -

seperatorView.hidden = indexPath.row != 0

Upvotes: 4

Related Questions