In swift I have two view controllers. One is a tableView and displays data from an array. On a function call the other view controller calls the tableView and delivers arguments with new data for the table view. Even when I update the array buckets
where the data is taken from the TableView doesn't update. How do I get the program to reload its data?
TABLE VIEW
import Foundation
import UIKit
class BucketTableViewController: UITableViewController {
var buckets = ["A", "B", "C"]
override func viewDidLoad() {
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return buckets.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = buckets[indexPath.row]
return cell
}
func updateBuckets(newBucket: NSArray) {
buckets = newBucket as! [String]
self.tableView?.reloadData()
}
}
I can verify that updateBuckets is called from the other view controller.
OTHER VIEW CONTROLLER
var bigArray = ["M", "A", "R", "C"]
override func viewDidLoad() {
super.viewDidLoad()
let buckets = BucketTableViewController()
buckets.updateBuckets(bigArray)
}
Try using tableView?.beginUpdates()
and tableView?.endUpdates()
instead - I've found that tableView?.reloadData()
can be finicky about data source updates sometimes.
Otherwise, make sure you're calling updateBuckets()
on the right instance of BucketTableViewController()
; it looks like you instantiated a new instance in your viewDidLoad() instead of changing the content of an existing instance (as you presumably would be doing).
This line right here:
let buckets = BucketTableViewController()
You're creating a brand new BucketTableViewController
instance and updating its buckets. That's not going to cause the existing BucketTableViewController
to update. You need to figure out how to get a reference to the existing view controller and update it instead.
我会为您推荐与在此答案中所做的相同的事情: 从其他文件更新表视图-无法在没有参数的情况下调用'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.