0

Hi guys I have a problem. After many videos i haven't understand how can I use the Core Data to store an array.

I have an application where in a view I putted 16 TextLabel. I need to use Core Data to Store a list of string. when I launch the program I need to load the data from the array. But sometimes I need to change the data of one TextLabel with a text from a text field, and then reload all the 16 text label.

Can someone help me with the code??

Sorry but I haven't understood How it works.

I have already create the project with core data. thanks at all

Alan Francis
  • 1,249
  • 11
  • 17
Twing90
  • 407
  • 2
  • 5
  • 15

2 Answers2

1

First suggestion: replace all your outlets with labels with tags. This will produce less code which will be more readable. You can fill in your labels with convenient loops.

E.g., your "primary" labels could have tags 10-17,
your "secondary" labels could have tags 20-27.

To get a specific label just use

let label = view.viewWithTag(20) as! UILabel

Second, for storing an array of 16 strings, use NSUserDefaults which is much simpler and built for this kind of data volume and type.

NSUserDefaults.standardUserDefaults().setObject(array, forKey: "myArray")
Mundi
  • 79,884
  • 17
  • 117
  • 140
-1

there is my code. In this view I want to set all the 16 text label using an array. when I press the button under one of these text label i need to change the data whit a string from a granigliatoreTF and then reload all the 16 text labels. I have implemented only one button for start. Thanks to all

import UIKit import CoreData

class DisposizioneGranigliatori: UIViewController, UIPickerViewDelegate , UIPickerViewDataSource {

var disposizione = [NSManagedObject]()

@IBOutlet var granigliatoreTF: UITextField!
@IBOutlet var principale1LB: UILabel!
@IBOutlet var principale2LB: UILabel!
@IBOutlet var principale3LB: UILabel!
@IBOutlet var principale4LB: UILabel!
@IBOutlet var principale5LB: UILabel!
@IBOutlet var principale6LB: UILabel!
@IBOutlet var principale7LB: UILabel!
@IBOutlet var principale8LB: UILabel!

@IBOutlet var secondario1LB: UILabel!
@IBOutlet var secondario2LB: UILabel!
@IBOutlet var secondario3LB: UILabel!
@IBOutlet var secondario4LB: UILabel!
@IBOutlet var secondario5LB: UILabel!
@IBOutlet var secondario6LB: UILabel!
@IBOutlet var secondario7LB: UILabel!
@IBOutlet var secondario8LB: UILabel!

@IBOutlet var modifiche: UIView!

@IBOutlet weak var picker: UIPickerView!

var pickerData : [[String]] = [[String]]()

override func viewDidLoad() {
    super.viewDidLoad()

    modifiche.hidden = true


    pickerData = [["12","161","164","165","176","177","245","246","247","250","255","256","262","263","266","267","268","269","270","271","274","275","278","287","289","290","293","301","303","306","307","308","328","330","336","337","340","344","346","347","352","355","357","374","377","381","383","387","391","394","395","399","400","401","403","417","421","423","430","F365","TGF 2666","FTS 2061"],["00","0,09","0,10","0,12","0,12-00","1400","Z","0,14-0,12","0,14","0,16","0,18","0,21","0,25","0,31","0,40","0,52","0,71"]]

    self.picker.delegate = self
    self.picker.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return pickerData.count
}

// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData[component].count
}

// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[component][row]
}

}


@IBAction func bigbagPrinc1(sender: AnyObject) {
}

@IBAction func bigbagSec1(sender: AnyObject) {
}

}

Twing90
  • 407
  • 2
  • 5
  • 15