简体   繁体   中英

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.

在此处输入图片说明

Here is the console dump after hitting save.

在此处输入图片说明

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

}
}

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM