【问题标题】:How to save UIStepper to CoreData (swift 3)如何将 UIStepper 保存到 CoreData (swift 3)
【发布时间】:2017-03-15 21:42:49
【问题描述】:

我在 Tapleviewcell 中使用 UIStepper,但我不知道如何在我的 CoreData 中保存步进值?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: StepperTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! StepperTableViewCell

    //stepper is your UIStepper reference from StepperTableViewCell
    cell.stepper.tag = indexPath.row
    cell.stepper.addTarget(self, action: #selector(stepperAction(sender:)), for: .valueChanged)

    return cell
}
func stepperAction(sender: UIStepper)  {
    print("Stepper \(sender.tag) clicked. Its value \(sender.value)")
}

【问题讨论】:

标签: core-data swift3 uistepper


【解决方案1】:

1.您像这样在 StepperTableViewCell 中创建一个变量

var callBackStepper:((_ value:Double)->())?

2.您在 StepperTableViewCell 中创建一个 IBACTION(不要忘记引用您的 UI) 可能看起来像这样:

@IBAction func stepperTapped(sender: AnyObject) {
   callBackStepper?(sender.value)
}
  1. 在你的 UIViewController 中你设置回调的 tableView

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: StepperTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! StepperTableViewCell
    
    cell.callBackStepper = { value in
       print("every time called when you use UIStepper \(value)")
     }
    return cell
    }
    

它未经测试,但它应该可以工作,请提供反馈

【讨论】:

  • 我还没有测试,但是如何保存 CoreData 中每个单元格的步进值?
  • 我以为你知道当你拥有价值时如何持久保存价值?您总是在回调中获得值。要永久保存它,请查看 stackoverflow.com/questions/37958530/using-double-in-core-da‌​ta 请先测试代码!
  • 我测试了它,现在我有了价值,但我在每个单元格中都有价值,因为我的项目中有 tableview,所以如何使用每个单元格中的每个新步进值更新 CoreData。
  • 你能用 github 或 zip 分享你的项目吗?
  • 理解起来很复杂:(但是我有一个表格视图,并且在每个单元格中都有步进器,现在我可以获得步进器值但我不知道如何更新/编辑旧值核心数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 2014-09-27
  • 1970-01-01
  • 2015-07-01
  • 1970-01-01
相关资源
最近更新 更多