jonask
jonask

Reputation: 748

Updating tableview after adding object

Im saving strings with coredata, and all my saved strings, will be shown as labeltext in the tableview. The strings comes from the textfield in alertview. It saves the strings correctly, but i have to close and run the project again, before the new string will be shown in the tableview.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!

var buyingList : [BuyList] = []

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    gettingData()


    self.tableView.dataSource = self
    self.tableView.delegate = self

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.buyingList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   // let Cell = tableView.dequeueReusableCellWithIdentifier("identifier") as UITableViewCell!
    let Cell = UITableViewCell()



    let tableViewData = self.buyingList.reverse()[indexPath.row]
    Cell.textLabel?.text = tableViewData.buyList

    return Cell
}

func refresh(){
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        dump(self.buyingList)
        self.tableView.reloadData()
    }
}

@IBAction func addButton(sender: UIBarButtonItem) {
    nameOfPicture()
}

func nameOfPicture() {
    let alert = UIAlertController(title: "Tilføj", message: "", preferredStyle: UIAlertControllerStyle.Alert)

    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
    alert.addAction(cancel)

    alert.addTextFieldWithConfigurationHandler { (textField) -> Void in

    }

    alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in

        let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

        let savingData = NSEntityDescription.insertNewObjectForEntityForName("BuyList", inManagedObjectContext: context) as! BuyList

        let textf = alert.textFields![0] as UITextField

        savingData.buyList = textf.text

        do {
            try context.save()

        } catch _ {

        }

    }))

    self.presentViewController(alert, animated: true, completion: nil)
}

func gettingData() {
    let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    let request = NSFetchRequest(entityName: "BuyList")

    var results : [AnyObject]?

    do {
        results = try context.executeFetchRequest(request)
    } catch _ {
        results = nil
    }

    if results != nil {
        self.buyingList = results! as! [BuyList]
    }
}
}

Upvotes: 0

Views: 286

Answers (1)

DonBaron
DonBaron

Reputation: 1374

The tableview is not reloaded after you fetched your data. Your getting data function should look like that:

if results != nil {
   self.buyingList = results! as! [BuyList]
   self.tableView.reloadData()
}

if it doesn't display the data in your table after this call, it means your results ergo your buyingList is empty

Upvotes: 1

Related Questions