[英]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.