Chas Burks
Chas Burks

Reputation: 47

Swift - tableView not reloading after adding to array

I am having an issue updating my UITableView. I have an app that has a basic UIViewController with a with a navigationItem to "Add New" items to the UITableViewContioller. I can add the string and then dump the array to see if it has been added. The dump function shows the added item in the array but the UITableView does not, even after calling reloadData. Here is my code.

enter image description here

Here is the console dump after hitting save.

enter image description here

So you can see something is there after I save but nothing shows up on the UITableview.

From the UIViewController class I have a UIAlertAction to accept text and a save button.

      let table = TableViewController()


        let saveAction = UIAlertAction(title: "Save", style:UIAlertActionStyle.Default, handler: {
        alert -> Void in

        let firstTextField = alertController.textFields![0] as UITextField
        self.table.cityArry.addObject(firstTextField.text!)
        self.table.refresh()
    })

And from UITableViewController class

 class TableViewController: UITableViewController {

var cityArry:NSMutableArray = ["Local"]

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self

}


func refresh(){
    dump(cityArry)
    tableView.reloadData()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cityArry.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
    let item = cityArry[indexPath.row]
    let cell = UITableViewCell()
    let label = UILabel(frame: CGRect(x:20, y:0, width:200, height:50))
    label.text = item as? String
    cell.addSubview(label)

    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    // Nothing here yet

}
}

Upvotes: 0

Views: 2739

Answers (3)

sschale
sschale

Reputation: 5188

I don't think that you should have two separate view controllers for the screen you're showing. Simplification may end up, as @J.Wang alludes to, solve your problem of calling methods on different objects. Your screen should be well handled with a TableViewController embedded in a Navigation Controller that has a button added to it.

Upvotes: 1

theMikeSwan
theMikeSwan

Reputation: 4729

Try changing this:

func refresh(){
    dump(cityArry)
    tableView.reloadData() 
}

to this:

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

If it happens that refresh() is getting called on anything other than the main thread the table view will likely not update properly. Might not be the issue but its a good first step.

Upvotes: 0

Sahil
Sahil

Reputation: 9226

you need to reload data in main thread in refresh function.

dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})

Upvotes: 0

Related Questions