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

时间:2019-01-24 15:23:31

标签: ios swift uicollectionview

我有一个自定义对象数组生成的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()

1 个答案:

答案 0 :(得分:0)

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

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)
}