Reputation: 2537
I have a UITableView
which has two type of background color (Gray and White). I'm giving colors by looking If indexPath.row
is odd or even. Everything looks okey at first but when I scroll it, some ordered cells look same color. When I debug, It goes inside CellForRowAt
function and related code but cell background color doesn't change.
TableView Functions:
extension SupportedServicesViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let data = dataSource{
return data.count
}else{
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "supportedServicesCell", for: indexPath) as? SupportedServicesTableViewCell else { fatalError("Supported Services Cell Error")}
guard let data = self.dataSource else { return cell }
cell.cell_data = data[indexPath.row]
if(indexPath.row % 2 == 0){
cell.contentView.backgroundColor = Color.SupportedServicesTable.cellGray
cell.customAccessoryView.backgroundColor = Color.SupportedServicesTable.accessoryGray
}else{
cell.containerView.backgroundColor = UIColor.white
cell.customAccessoryView.backgroundColor = Color.SupportedServicesTable.cellGray
}
return cell
}
}
Custom Cell Class:
class SupportedServicesTableViewCell: BaseCell {
var cell_data: SupportedService?{
didSet{
guard let unwrappedCell = cell_data else { return }
self.cellText.text = unwrappedCell.title
self.logoImageView.image = UIImage(named: "anlasmali-servisler")?.withRenderingMode(.alwaysOriginal)
self.logoImageView.contentMode = .scaleAspectFit
}
}
let containerView = UIView()
let logoImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
return imageView
}()
let customAccessoryView = UIView()
override init(style: UITableViewCellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
override func setupViews() {
self.backgroundColor = UIColor.white
//Right Arrow For Cell
let image = UIImage(named: "forward-turkuaz")?.withRenderingMode(.alwaysOriginal)
let checkmark = UIImageView()
checkmark.tintColor = Color.NavigationBar.tintColor
checkmark.image = image
self.customAccessoryView.addSubview(checkmark)
checkmark.anchorCenterSuperview()
self.cellText.numberOfLines = 2
self.cellText.adjustsFontSizeToFitWidth = true
self.cellText.font = Fonts.robotoFont
self.containerView.addSubview(logoImageView)
self.containerView.addSubview(cellText)
self.containerView.addSubview(customAccessoryView)
self.addSubview(containerView)
logoImageView.anchor(self.containerView.topAnchor, left: self.containerView.leftAnchor, bottom: self.containerView.bottomAnchor, right: nil, topConstant: 14, leftConstant: 14, bottomConstant: 14, rightConstant: 0, widthConstant: 24, heightConstant: 0)
cellText.anchor(self.containerView.topAnchor, left: logoImageView.rightAnchor, bottom: self.containerView.bottomAnchor, right: self.customAccessoryView.leftAnchor, topConstant: 0, leftConstant: 20, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
customAccessoryView.anchor(self.containerView.topAnchor, left: nil, bottom: self.containerView.bottomAnchor, right: self.containerView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: self.frame.height, heightConstant: 0)
containerView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 2, leftConstant: 5, bottomConstant: 2, rightConstant: 2, widthConstant: 0, heightConstant: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Upvotes: 0
Views: 898
Reputation: 793
It may happening because you have not cleared the cell in prepareForReuse:
override func prepareForReuse() {
super.prepareForReuse();
// Do the minor cleanup that is needed to reuse the cell
self.contentView.backgroundColor = .clear;
self.customAccessoryView.backgroundColor = .clear;
}
In the cellForRowAtIndexpath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "supportedServicesCell", for: indexPath) as? SupportedServicesTableViewCell else { fatalError("Supported Services Cell Error")}
guard let data = self.dataSource else { return cell }
cell.cell_data = data[indexPath.row]
if(indexPath.row % 2 == 0){
cell.contentView.backgroundColor = Color.SupportedServicesTable.cellGray
cell.customAccessoryView.backgroundColor = Color.SupportedServicesTable.accessoryGray
cell.containerView.backgroundColor = Color.SupportedServicesTable.accessoryGray // Set the color you want
}else{
cell.containerView.backgroundColor = UIColor.white
cell.customAccessoryView.backgroundColor = Color.SupportedServicesTable.cellGray
cell.contentView.backgroundColor = .white // Set the color you want
}
return cell
}
}
Upvotes: 3