【问题标题】:Take data from UITabeVieCell to populate to another ViewController从 UITabeVieCell 获取数据以填充到另一个 ViewController
【发布时间】: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


    【解决方案1】:

    这归结为“我如何在我的应用程序的不同组件之间进行通信”。因为我假设您不仅想通知您的 ViewController 颜色更改,而是更新整个 UI(如应用主题),最简单的方法是使用 NotificationCenter 并为其提供颜色以便所有组件对变化感兴趣的可以观察一下。

    public extension NSNotification.Name {
      static let ColorChanged = Self(rawValue: "color_changed")
    }
    
    @objc func didTabSwitch(_ sender: UISwitch){
      if sender.isOn {
        NotificationCenter.default.post(name: .ColorChanged, object: UIColor.randomFlat)
      }
      else {
        NotificationCenter.default.post(name: .ColorChanged, object: nil)
      }
    }
    

    ViewController(或其他感兴趣的组件)中

    private var chameleonColor: UIColor? {
      didSet {
        tableView.reloadData()
      }
    }
    
    override func viewDidLoad() {
      super.viewDidLoad()
     
      NotificationCenter.default.addObserver(
        self,
        selector: #selector(updateColor),
        name: .ColorChanged,
        object: nil
      )
    }
    
    @objc private func updateColor(_ notification: Notification) {
      chameleonColor = notification.object as? UIColor
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      // ...
      cell.backgroundColor = chameleonColor ?? .systemBackgroundColor
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2022-01-13
      • 1970-01-01
      • 2020-05-31
      相关资源
      最近更新 更多