我的情况是,我正在尝试根据主题更改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")
}
}
}
答案 0 :(得分:0)
就在调用委托之前调用以下方法,并且tableView的dataSource可能在如下所示的ViewDidLaod示例中。
self.themeValidation(theme:"1")
tableView.delegate=self
tableView.dataSource=self
OR
您也可以在默认情况下设置颜色,因此,如果找不到任何主题,则应从默认情况下设置颜色
答案 1 :(得分:0)
如果您呼叫tableView.dequeueReusableCell
,则会创建一个新的单元格。您需要传入实际单元格(或至少传入其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:)
。
答案 2 :(得分:0)
这是您可以做的。
为主题创建变量
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
}
}