Alfredo Liardo Alfredo Liardo - 4 months ago 17
Swift Question

TableView is not updated with the new cells

I'm making a simple app that display various cells , one for capture, in a table view.
For testing purpose I have add some example cells that are loaded from

var captures = [Capture]()


and it works.
When i try to add new captures into captures array they are not displayed.
This is my CapturesTableViewController.swift:

import UIKit

class CapturesTableViewController: UITableViewController {

// MARK: Properties
var capture : Capture!
var captures = [Capture]()

override func viewDidLoad() {
super.viewDidLoad()

// Load the sample data.
loadSampleCaptures()

}

func loadSampleCaptures() {
let photo1 = UIImage(named: "photo1")
let capture1 = Capture(photo: photo1, name: "Barracuda", weight: 0.8, bait: "Duo tide flyer", notes: "prima cattura")

let photo2 = UIImage(named: "photo2")
let capture2 = Capture(photo: photo2, name: "Barracuda", weight: 1.2, bait: "Nemesi 18g", notes: "")

let photo3 = UIImage(named: "photo3")
let capture3 = Capture(photo: photo3, name: "Spigola", weight: 1.5, bait: "assassino", notes: "prima spigola")

captures += [capture1!,capture2!,capture3!]
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return captures.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "CaptureTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CaptureTableViewCell

// Fetches the appropriate meal for the data source layout.
let capture = captures[indexPath.row]

cell.fishName.text = capture.fishName
cell.fishPhoto.image = capture.photo
cell.fishWeight.text = "\(capture.fishWeight) Kg"

return cell
}



@IBAction func unwindToCaptureList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? NuovaCatturaViewController , capture = sourceViewController.capture{
// Add a new capture
let newIndexPath = NSIndexPath(forRow: captures.count, inSection: 0)
captures.append(capture)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Right)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}

}


enter image description here

I call unwindToCaptureList method from another scene and the if statement is executed and the capture is not nil.
Why is it not displayed?
Yet I am following the official tutorial apple (Food Tracker App) as a guideline.

UPDATE:
I have tried to add more entry and on the third entry it start to work fine.
enter image description here
How is it possible?

Answer

Have you connected your method to your other view and do you know if it is executing? Add a print("Test") to check if the method is executed like below

@IBAction func unwindToCaptureList(sender: UIStoryboardSegue) {
        if let sourceViewController = sender.sourceViewController as? NuovaCatturaViewController , capture = sourceViewController.capture{
            // Add a new capture
            print("Is this executed?: \(capture)")
            print("Check dataSource before appending: \(self.captures)")
            self.captures.append(capture)
            print("Check dataSource after appending: \(self.captures)")
            self.tableView.reloadData()
        }
    }