Reputation: 61
I have the following function:
func createTableViewCard() {
let myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
let newView = UIView(frame: CGRect(x: 10, y: 10, width: screenSize.width-20, height: screenSize.height-20))
newView.addSubview(myTableView)
view.addSubview(newView)
}
Now, inside my viewDidLoad()
, I would like to "reloadData" for this tableView that I just added to the main view.
How can I do that?
Upvotes: 0
Views: 665
Reputation: 1383
First declare your table view instance globally
var myTableView :UITableView!
until and unless you don't assign the delegate and datasource reload function does not work
func createTableViewCard() {
myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
let newView = UIView(frame: CGRect(x: 10, y: 10, width: screenSize.width-20, height: screenSize.height-20))
newView.addSubview(myTableView)
myTableView.delegate = self
myTableView.datasource = self
view.addSubview(newView)
}
later your can reload data easily
myTableView.reloadData()
Upvotes: 0
Reputation: 3013
Try something like this.Call createTableViewCard function when you wanna add tableView into UIView.Call refreshUI function when you need to reload
class YOUR_CLASS {
let myTableView = UITableView()
ovverride func viewDidLoad(){
self.createTableViewCard() // call this method when you want add tableView
}
func createTableViewCard() {
self.myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
let newView = UIView(frame: CGRect(x: 10, y: 10, width: screenSize.width-20, height: screenSize.height-20))
newView.addSubview(myTableView)
view.addSubview(newView)
}
func refreshUI() {
dispatch_async(dispatch_get_main_queue(),{
self.myTableView.reloadData()
});
}
}
Upvotes: 0
Reputation: 2713
Make myTableView
a property of your class and reload it on the main thread:
func refreshUI() {
dispatch_async(dispatch_get_main_queue(),{
self.myTableView.reloadData()
});
}
Upvotes: 2