【发布时间】:2021-03-06 03:18:03
【问题描述】:
我已将我的应用程序添加到 Chameleon 框架以生成随机颜色,我想将内部设置添加到我的应用程序。例如,我创建了一个 tabelViewController VC,然后我添加了一个开关,当 mySwitch isOn 我希望变色龙颜色填充 tableViewCells 时,我希望 tableViewCells 为 .systemBackgoundColor。
我的设置VC
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Chameleon Color"
let mySwitch = UISwitch()
mySwitch.onTintColor = .blue
mySwitch.addTarget(self, action: #selector(didTabSwitch(_:)), for: .valueChanged)
cell.accessoryView = mySwitch
return cell
}
@objc func didTabSwitch(_ sender: UISwitch){
if sender.isOn {
// populate the chameleon framework color
}
else {
// change to the default background color
}
}
视图控制器
/// Provides a cell object for each row.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Fetch a cell of the appropriate type.
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = list[indexPath.row].name
cell.backgroundColor = UIColor(hexString: list[indexPath.row].color)
cell.textLabel?.textColor = UIColor(contrastingBlackOrWhiteColorOn: cell.backgroundColor, isFlat: true)
if let date = list[indexPath.row].date {
let formater = DateFormatter()
formater.timeZone = .current
formater.locale = .current
formater.timeStyle = .short
formater.dateStyle = .none
cell.detailTextLabel?.text = formater.string(from: date)
cell.detailTextLabel?.textColor = UIColor(contrastingBlackOrWhiteColorOn: cell.backgroundColor, isFlat: true)
}
return cell
}
任何帮助或提示将不胜感激。
【问题讨论】:
标签: ios swift xcode uitableview settings