使用调用自定义 tableview 单元格的函数简化 tableview 代码

Posted

技术标签:

【中文标题】使用调用自定义 tableview 单元格的函数简化 tableview 代码【英文标题】:Simplifying tableview code with function calling custom tableview cell 【发布时间】:2017-08-21 23:35:27 【问题描述】:

希望通过在我的 tableview 函数中使用一个函数来简化我的代码。我认为这是可能的,但我什至不知道如何开始。我尝试创建一个函数来执行我想要的操作,但我不知道如何在我的函数中调用自定义单元格变量。

这是我要简化的代码:

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

    if indexPath.section == 0 
        // PART 1
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell1", for: indexPath) as! PaydayDetailCellA1
        let (jobDescription, day1, day2, day3, day4, day5, day6, day7, _) = tempPaydayDailyJobs[indexPath.row]

        cell.tallyView.layer.cornerRadius = cell.tallyView.bounds.height / 6.4
        cell.tallyView.layer.masksToBounds = true
        cell.tallyView.layer.borderColor = UIColor.lightGray.cgColor
        cell.tallyView.layer.borderWidth = 0.5

        cell.jobDesc.text = jobDescription

        cell.dayColor1.text = day1
        switch day1 
        case "1":
            cell.dayColor1.text = ""
            cell.dayColor1.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
        case "X":
            cell.dayColor1.text = ""
            cell.dayColor1.backgroundColor = UIColor.red
        case "E":
            cell.dayColor1.backgroundColor = UIColor.lightGray
        default:
            cell.dayColor1.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
            cell.dayColor1.textColor = UIColor.white
        

        cell.dayColor2.text = day2
        switch day2 
        case "1":
            cell.dayColor2.text = ""
            cell.dayColor2.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
        case "X":
            cell.dayColor2.text = ""
            cell.dayColor2.backgroundColor = UIColor.red
        case "E":
            cell.dayColor2.backgroundColor = UIColor.lightGray
        default:
            cell.dayColor2.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
            cell.dayColor2.textColor = UIColor.white
        

        cell.dayColor3.text = day3
        switch day3 
        case "1":
            cell.dayColor3.text = ""
            cell.dayColor3.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
        case "X":
            cell.dayColor3.text = ""
            cell.dayColor3.backgroundColor = UIColor.red
        case "E":
            cell.dayColor3.backgroundColor = UIColor.lightGray
        default:
            cell.dayColor3.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
            cell.dayColor3.textColor = UIColor.white
        

        cell.dayColor4.text = day4
        switch day4 
        case "1":
            cell.dayColor4.text = ""
            cell.dayColor4.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
        case "X":
            cell.dayColor4.text = ""
            cell.dayColor4.backgroundColor = UIColor.red
        case "E":
            cell.dayColor4.backgroundColor = UIColor.lightGray
        default:
            cell.dayColor4.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
            cell.dayColor4.textColor = UIColor.white
        

        cell.dayColor5.text = day5
        switch day5 
        case "1":
            cell.dayColor5.text = ""
            cell.dayColor5.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
        case "X":
            cell.dayColor5.text = ""
            cell.dayColor5.backgroundColor = UIColor.red
        case "E":
            cell.dayColor5.backgroundColor = UIColor.lightGray
        default:
            cell.dayColor5.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
            cell.dayColor5.textColor = UIColor.white
        

        cell.dayColor6.text = day6
        switch day6 
        case "1":
            cell.dayColor6.text = ""
            cell.dayColor6.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
        case "X":
            cell.dayColor6.text = ""
            cell.dayColor6.backgroundColor = UIColor.red
        case "E":
            cell.dayColor6.backgroundColor = UIColor.lightGray
        default:
            cell.dayColor6.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
            cell.dayColor6.textColor = UIColor.white
        

        cell.dayColor7.text = day7
        switch day7 
        case "1":
            cell.dayColor7.text = ""
            cell.dayColor7.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
        case "X":
            cell.dayColor7.text = ""
            cell.dayColor7.backgroundColor = UIColor.red
        case "E":
            cell.dayColor7.backgroundColor = UIColor.lightGray
        default:
            cell.dayColor7.backgroundColor = UIColor(red: 141/255, green: 198/255, blue: 63/255, alpha: 1)
            cell.dayColor7.textColor = UIColor.white
        

        return cell

     else 
        // PART 2
        let cell2 = tableView.dequeueReusableCell(withIdentifier: "CustomCell2", for: indexPath) as! PaydayDetailCellA2
        let (_, pointAmount) = tempDailyChoresSummary[indexPath.row]

        cell2.dailyChoresNumber.text = "\(pointAmount)"
        cell2.jobConsistencyBonusNumber.text = "\(pointAmount)"
        cell2.previousUnpaidAmountsNumber.text = "\(pointAmount)"
        cell2.dailyChoresSubtotalNumber.text = "\(pointAmount)"

        return cell2
    

我在自定义表格视图单元格中有一块标签。我希望标签的背景颜色根据外部数组中的数据改变颜色。我附上了一张它的样子(如果有帮助的话)。 Here is the picture of the tableview with colored labels.

或者有没有更简单的方法来做我想做的事情?

【问题讨论】:

【参考方案1】:

这种方法可以减少表函数中的代码。请记住,这是一个概念。 您可以通过创建 UITableViewCell 的扩展来打破一些重复的属性设置。

首先创建一个协议

protocol Settable 
    func setData(val: Int)

然后做一个扩展

extension Settable 
    func setData(jobDescription: String, jobSubtotal: String, cornerRadius: Float, maskToBounds: Bool, borderColor: CGColor, borderWidth: Float) 
        self.tallyView.layer.cornerRadius = cornerRadius
        self.tallyView.layer.masksToBounds = maskToBounds
        self.tallyView.layer.borderColor = borderColor
        self.tallyView.layer.borderWidth = borderWidth
        self.jobDesc.text = jobDescription
        self.jobSubtotal.text = jobSubtotal
    

然后扩展你的 UITableViewCell

class PaydayNewCellA1 : UITableViewCell, Settable 


class PaydayNewCellB1 : UITableViewCell, Settable 


class PaydayNewCellC1 : UITableViewCell, Settable 


然后你可以在你的函数中执行以下操作:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    if (tableView == self.tableView1) 
        if indexPath.section == 0 
            let cell = tableView1.dequeueReusableCell(withIdentifier: "CustomCell1", for: indexPath) as! PaydayNewCellA1
            // Now use the function
            cell.setData(jobDescription: "daily job \(indexPath.row + 1)", jobSubtotal: "50", cornerRadius: cell.tallyView.bounds.height / 6.4, maskToBounds: true, borderColor: UIColor.lightGray.cgColor, borderWidth: 0.5)
            return cell
         else 
            let cell = tableView1.dequeueReusableCell(withIdentifier: "CustomCell2", for: indexPath) as! PaydayNewCellA2
            return cell
        
     else if (tableView == tableView2) 

     // rest of code etc


【讨论】:

谢谢,但正如以前的用户注意到的那样,我发布了错误的代码,所以你的回答虽然很好,但并不能解决我的问题。请查看更新后的代码。 (我可能在您发布答案的同时发布了它,因此您从未看到更新的代码)。

以上是关于使用调用自定义 tableview 单元格的函数简化 tableview 代码的主要内容,如果未能解决你的问题,请参考以下文章

如果我有自定义单元格,我可以在 heightForRowAtIndexPath 中调用 tableView cellForRowAtIndexPathtableView 来查找单元格的大小吗?

从自定义单元格重新加载表格视图数据

自定义 Tableview 单元格的问题

如何在 TableView 中调整自定义单元格的大小?

单击自定义单元格的 UITextField 后重新加载 TableView

表格内容在使用子视图的自定义单元格的 TableView 滚动上消失 - Swift