Dequeue Custom TableViewCell无法正常工作

时间:2018-01-27 23:27:43

标签: swift xcode uitableview tableview

在我的项目中,我在TableViewController中设置了自定义tableViewCell。 在我的自定义单元格中,我设置了一个标签,表示一个类别,一个标签表示当前类别的总和(例如,类别“假期”,总和是300美元)和一个ImageView,它应该表示相对于每个类别的总和category ..这是它应该是什么样子的一个例子 - >

enter image description here

问题是我以编程方式为我的imageView设置了约束,当我向下滚动然后再向上时,约束无法正常工作。 这是cellForRowAtIndex

中的代码
let quantity: [Double] = [100, 60, 40, 15, 10, 5, 0, 0, 0, 0, 0]
let categories: [String] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "L", "M"]
let total: Double = 230

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyTableViewCell

    cell.labelCategory.text = "Category \(categories[indexPath.row])"
    cell.labelQuantity.text = "Quantity: \(quantities[indexPath.row])"

    let width = cell.frame.width

    let percentageImageView: UIImageView = {
        let view = UIImageView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.image = UIImage(named: "percentage")
        view.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: width, height: 20))
        return view
    }()

    cell.addSubview(percentageImageView)

    let percentage = (quantity[indexPath.row] / total)


    percentageImageView.leftAnchor.constraint(equalTo: cell.leftAnchor, constant: 8).isActive = true
    percentageImageView.topAnchor.constraint(equalTo: cell.labelCategory.bottomAnchor, constant: 8).isActive = true
    percentageImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    percentageImageView.widthAnchor.constraint(equalToConstant: width * CGFloat(percentage)).isActive = true

    return cell
}

问题是滚动后一些单元格显示百分比宽度错误的图像,例如在下面的照片中,类别“M”和“L”不应该有imageView,因为宽度为0.0。

enter image description here

1 个答案:

答案 0 :(得分:-2)

我解决了我的问题,即创建一个自定义UITableViewCell数组并从数组加载单元格。

var arrayOfCells: [MyTableViewCells] = []

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

    if arrayOfCells.count == quantity.count {
        return arrayOfCells[indexPath.row]!
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyTableViewCell

        cell.labelCategory.text = "Category \(categories[indexPath.row])"
        cell.labelQuantity.text = "Quantity: \(quantities[indexPath.row])"

        let width = cell.frame.width

        let percentageImageView: UIImageView = {
           let view = UIImageView()
            view.translatesAutoresizingMaskIntoConstraints = false
            view.image = UIImage(named: "percentage")
            view.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: width, height: 20))
            return view
        }()

        cell.addSubview(percentageImageView)

        let percentage = (quantity[indexPath.row] / total)


        percentageImageView.leftAnchor.constraint(equalTo: cell.leftAnchor, constant: 8).isActive = true
        percentageImageView.topAnchor.constraint(equalTo: cell.labelCategory.bottomAnchor, constant: 8).isActive = true
        percentageImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
        percentageImageView.widthAnchor.constraint(equalToConstant: width * CGFloat(percentage)).isActive = true

        arrayOfCells.append(cell)
        return cell
    }
}

然后在viewWillAppear中,我只是清空单元格数组并重新加载tableView。

不知道它是否是一种好用的方法,但似乎有用。