var late do n't wok

时间:2019-05-07 17:06:57

标签: ios swift

当变量跳动==“ true”时,文本必须为红色

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

        if self.switchInstallmentToPay == true {
            if let installment = PaymentManager.paymentPlan?.unpaidInstallments![indexPath.row] {
                if let id = installment.id, let paymentDue = installment.paymentDue, let description = installment.numberDescription, let method = installment.paymentMethodDescription, let expectedPayment = installment.expectedPayment, let actualPayment = installment.actualPayment, let payable = installment.payable, let late = installment.late {
                    cell.load(id: id, paymentDue: paymentDue, description: description, method: method, expectedPayment: expectedPayment, actualPayment: actualPayment, payable: payable, late: late)
                    if installment.payable! {
                        cell.accessoryType = .checkmark
                        cell.tintColor = UIColor.lighterGray
                        cell.isUserInteractionEnabled = true
                        if installment.late! {
                            cell.lbDescription.textColor = UIColor.danger // not working
                        }

                    }else{
                        cell.accessoryType = .none
                        //cell.tintColor = UIColor.lightGray
                        cell.isUserInteractionEnabled = false
                    }
                }
            }
        }else{
            if let installment = PaymentManager.paymentPlan?.paidInstallments![indexPath.row] {
                if let id = installment.id, let paymentDue = installment.paymentDue, let description = installment.numberDescription, let method = installment.paymentMethodDescription, let expectedPayment = installment.expectedPayment, let actualPayment = installment.actualPayment, let payable = installment.payable, let late = installment.late {
                    cell.load(id: id, paymentDue: paymentDue, description: description, method: method, expectedPayment: expectedPayment, actualPayment: actualPayment, payable: payable, late: late)
                    cell.accessoryType = .none
                    cell.isUserInteractionEnabled = false

                    cell.lbDescription.textColor = UIColor.black // not working

                cell.tintColor = UIColor.lighterGray
            }
        }
    }
    return cell
}

1 个答案:

答案 0 :(得分:1)

此代码很难阅读,并且有很多冗余。如果您使用的是情节提要,我建议为已付款和未付款分期制作单独动态单元格。 两个单元格的类类型都可以保留InstallmentTableViewCell,因为您只是在复制单元格的视图,而不是它们的逻辑。您可以在情节提要的单元格原型中设置各种元素的颜色和样式,然后将tableView(_:cellForRowAt:indexPath)简化为

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellID = switchInstallmentToPay ? "unpaidCell" : "paidCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! InstallmentTableViewCell
    cell.load(...)

    return cell
}

我还建议更改cell.load()以接受一个installment参数并在那里设置单元格的属性,而不是使调用者陷入多个if let的混乱之中。