0

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

}
}
1
  • You sure the table displaying on your screen is the one you call refresh on? Commented Mar 1, 2016 at 5:33

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.