繁体   English   中英

保存来自uicollectionview单元更新的数据

[英]Saving data from updates of a uicollectionview cell

我有一个自定义对象数组生成的collectionview,其中每个单元格代表一个计数器,该计数器的值可以通过按钮“ +”和“-”进行更新。

当我使用按钮更新此值时,它会发生外观变化,但不会保存到数组中,因此,当我重新启动应用程序时,我使用的是较旧的金额,之前我使用按钮进行了更改

var data: [CounterModel] = []

这是我的ViewController.swift中的数组

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CounterCell", for: indexPath) as! CounterCell
    cell.configure(with: data[indexPath.row])

    return cell
  }

这是我配置单元格的地方

 public func configure(with model: CounterModel) {
    nameLabel.text = model.name
    amountLabel.text = String(model.amount)
  }

  @IBAction func lessBtnTapped(_ sender: Any) {
    newAmount = Int(amountLabel.text!)

    if (newAmount == 0) {
      amountLabel.text = "\(newAmount!)"
    } else {
      amountLabel.text = "\(newAmount! - 1)"
    }
  }


  @IBAction func moreBtnTapped(_ sender: Any) {
    newAmount = Int(amountLabel.text!)

    amountLabel.text = "\(newAmount! + 1)"
  }

这是我配置单元格并更改值的地方(在CounterCell.swift中)

struct CounterModel: Codable {
  var name: String
  var amount: Int
}

这是我的CounterModel

编辑 (缺少信息)

我将计数器编码为Json并将其保存在userdefaults中:

func saveUserDefaults(counters: [CounterModel]) {
  let defaults = UserDefaults.standard

  let jsonEncoder = JSONEncoder()
  let jsonData = try? jsonEncoder.encode(counters)

  defaults.set(jsonData, forKey: "savedCounters")
}

func loadUserDefaults() -> [CounterModel] {
  let defaults = UserDefaults.standard
  var savedCounters: [CounterModel] = []

  guard let jsonData = defaults.object(forKey: "savedCounters") as? Data else { return savedCounters}

  let jsonDecoder = JSONDecoder()
  savedCounters = try! jsonDecoder.decode([CounterModel].self, from: jsonData)

  return savedCounters

创建新计数器时,我会在AlertAction中更新Userdefaults

let newCounter: CounterModel = CounterModel(name: self.newLabelValue!, amount: self.newAmountValue!)

      self.data.append(newCounter)
      saveUserDefaults(counters: self.data)

      self.data = loadUserDefaults()

      self.collectionView.reloadData()

您必须更新模型。 使用闭包将分路器转移到视图控制器,然后在此处进行逻辑处理。 一个快速的解决方案可能是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CounterCell", for: indexPath) as! CounterCell
    cell.configure(with: data[indexPath.row]) { newValue in
        self.data[indexPath.row].amount = newValue
        self.saveUserDefaults(counters: self.data)
    }

    return cell
}

单元格类别:

private var valueDidChange: ((Int) -> Void)?

public func configure(with model: CounterModel, valueDidChange: @escaping (Int) -> Void) {
    nameLabel.text = model.name
    amountLabel.text = String(model.amount)
    self.valueDidChange = valueDidChange
}

@IBAction func lessBtnTapped(_ sender: Any) {
    newAmount = Int(amountLabel.text!)

    if (newAmount == 0) {
      amountLabel.text = "\(newAmount!)"
    } else {
      amountLabel.text = "\(newAmount! - 1)"
    }
    valueDidChange?(newAmount)
}


@IBAction func moreBtnTapped(_ sender: Any) {
    newAmount = Int(amountLabel.text!)

    amountLabel.text = "\(newAmount! + 1)"
    valueDidChange?(newAmount)
}

暂无
暂无

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

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