代码之家  ›  专栏  ›  技术社区  ›  Emre Önder

uitableview滚动时单元格背景色折叠

  •  0
  • Emre Önder  · 技术社区  · 7 年前

    我有一个 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 回复  |  直到 7 年前
        1
  •  2
  •   Pooja Gupta    7 年前

    可能是因为您尚未在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;
    }
    

    在牢房里

    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
      }
    }
    
    推荐文章