UITableView单元格背景颜色滚动时会折叠

时间:2018-07-06 08:28:00

标签: ios swift uitableview

我有一个UITableView,它有两种类型的背景色(灰色和白色)。我通过查看indexPath.row是奇数还是偶数来给出颜色。起初一切看起来不错,但是当我滚动它时,一些有序单元格看起来是相同的颜色。当我调试时,它进入了CellForRowAt函数和相关代码中,但是单元格背景色没有改变。

TableView函数:

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
}
}

自定义单元格类别:

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")
}
}

1 个答案:

答案 0 :(得分:2)

可能由于您没有清除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;
}

在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
  }
}