繁体   English   中英

Swift UITableview自定义单元格Imageview颜色更改的另一个功能

[英]Swift UITableview custom cell Imageview color change in another function

我的情况是,我试图根据主题更改UITableview自定义单元格Imageview图像的颜色。 在这里,我需要在另一个函数中访问UITableview自定义单元格Imageview而不传递Indexrow和sender。 我使用下面的代码,但tableview自定义单元格图像的颜色不变。

在我的代码下方

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
        self.themeValidation(theme:"1")
    }

    func themeValidation(theme:String){

            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell 

            // Validation based on color name
            switch theme {
            case "1" :
                cell.Icon.image = UIImage(named: "pic") 
                cell.Icon.setImageColors(color:  UIColor.blue) // Here not working
                self.navigationController?.navigationBar.tintColor = UIColor.blue
                break
            case "2" :
                cell.dateIcon.image = UIImage(named: "pic") // Here not working
                cell.Icon.setImageColors(color:  UIColor.red) 
                self.navigationController?.navigationBar.tintColor = UIColor.red
                break
            case "3" :
                cell.Icon.image = UIImage(named: "pic")
                cell.Icon.setImageColors(color:  UIColor.green) // Here not working
                self.navigationController?.navigationBar.tintColor = UIColor.green
                break
            default:
                print("Default")
            }
        }
    }

在调用委托之前立即调用以下方法,并且tableView的dataSource可能在如下的ViewDidLaod示例中。

self.themeValidation(theme:"1")
tableView.delegate=self
tableView.dataSource=self

要么

您也可以在默认情况下设置颜色,因此,如果找不到任何主题,则应从默认情况下设置颜色

如果调用tableView.dequeueReusableCell ,则会创建一个新单元格。 您需要传入实际单元格(或至少IndexPath它的IndexPath ,以便能够从tableView访问该单元格。

您应该将主题功能分解为单独的一个,用于表视图单元格,从cellForRowAt调用,然后从viewWillAppear调用。

一些进一步的改进:当您拥有一组有限且不可变的值时,应始终使用一个enum ,某些事物可以从中获取值,尤其是您的情况下的Theme

enum Theme {
    case blue
    case red
    case green

    var themeColor: UIColor {
        switch self {
        case .blue:
            return .blue
        case .red:
            return .red
        case .green:
            return .green
        }
    }
}

class VC: UITableViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setGlobalTheme(to: .blue)
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        setTheme(for: cell, to: .blue)
        return cell
    }

    func setGlobalTheme(to theme: Theme) {
        navigationController?.navigationBar.tintColor = theme.themeColor
    }

    func setTheme(for cell: CustomCell, to theme: Theme) {
        cell.Icon.image = UIImage(named: "pic")
        cell.Icon.setImageColors(color: theme.themeColor)
    }
}

如果您希望能够更改主题然后将这些更改应用于单元格,则需要在更改主题后调用tableView.reloadData ,甚至更好,请定义以下函数,该函数遍历所有可见单元格并进行更新。

func changeTheme(to theme: Theme) {
    setGlobalTheme(to: theme)
    tableView.visibleCells.forEach {
        guard let customCell = $0 as? CustomCell else { return }
        setTheme(for: customCell, to: theme)
    }
}

要更新主题时,只需确保调用changeTheme(to:)

这是您可以做的。

为主题创建变量

var theme:String

然后在viewWillApear或您设置主题的任何地方,设置此变量并调用主题更新方法

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.theme = "1"
    self.themeValidation()
}

您的themeValidation方法将如下所示

func themeValidation(theme:String){
//Only for view related changes and not cell related changes
    switch self.theme {
        case "1": //Do your changes related to view here
        case "2": //Do your changes related to view here

    }
}

现在在您的cellForRow方法中

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell 
    switch self.theme {
        case "1":
        cell.Icon.image = UIImage(named: "pic") 
        cell.Icon.setImageColors(color:  UIColor.blue)
        case "2":
        cell.Icon.setImageColors(color:  UIColor.red) 
        self.navigationController?.navigationBar.tintColor = UIColor.red
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM