2017-03-15 77 views
0

我在tapleviewcell中使用UIStepper,但我不知道如何在我的CoreData中保存步进值?如何将UIStepper保存到CoreData(swift 3)

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)") 
} 
+0

你想做些什么?只需保存sender.value?你真的需要核心数据吗?也许UserDefaults也会很棒?核心数据看看https://www.raywenderlich.com/145809/getting-started-core-data-tutorial或这里https://learnappdevelopment.com/ios-app-development-free/how-to -use-core-data-in-ios-10-swift-3 /或这里https://medium.com/ios-geek-community/beginners-guide-to-core-data-in-swift-3-85292ef4edd #.gnbnvyows – kuzdu

+0

我在我的项目中使用CoreData,步进器是它的一部分。我知道如何将文本从UITextfield保存到CoreData,但我不知道如何在CoreData中保存IUStepper值 – Alex

+0

总之我不明白这个问题。 sender.value是双精度或数字,不是吗?核心数据支持双倍。像字符串一样保存它。看到这里:http://stackoverflow.com/questions/37958530/using-double-in-core-data – kuzdu

回答

1

1.You在StepperTableViewCell创建一个变量,像这样

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

2.You创建您的StepperTableViewCell的IBAction为(不要忘记对您的UI的参考) 这可能有一个看起来像这样:

@IBAction func stepperTapped(sender: AnyObject) { 
    callBackStepper?(sender.value) 
} 
  • 在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 
    } 
    
  • 这是未经测试,但它应该工作,请给反馈

    +0

    我还没有测试它,但如何保存CoreData中每个单元格的步进值? – Alex

    +0

    我以为你知道如何在价值时持续保存价值?你总是在回调中获得价值。为了保存它持久看看stackoverflow.com/questions/37958530/using-double-in-core-da ta 请先测试代码! – kuzdu

    +0

    我测试了它,现在我有了价值,但是我在每个单元中都有价值,因为我在我的项目中使用了tableview,因此如何使用每个单元格中的每个新步进值更新CoreData。 – Alex

    相关问题