i'm quite new to this. I've spent some hours to go through the various questions on this topic but couldn't find an answer that fits to me question.
I have an viewcontroller (not a tableviewcontroller) with a tableview as subview. My question is how to reload the table data inside the :viewwillappear method of the view controller. Inside the method i can't "access" the tableview instance. (Also i understand i can't use reloaddata() because im not using a tableviewcontroller).
Do you know any simple solution? (i very much assume that a protocol could help here?)
this is the code in short:
class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
var Person_data = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
let tb: UITableView = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)
tb.dataSource = self
tb.delegate = self
super.view.addSubview(tb)
fetch_data()
}
func tableView(tableview: UITableView, numberOfRowsInSection section: Int) -> Int {
let rows = Person_data.count
return rows
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell = UITableViewCell()
cell.textLabel?.text = Person_data[indexPath.row].name
return cell
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetch_data()
// reload at this point
}
func fetch_data() {
let tb_fetchRequest = NSFetchRequest(entityName: "Person")
do {
let tb_fetchResults = try my_moc.executeFetchRequest(tb_fetchRequest) as? [Person]
Person_data = tb_fetchResults!
} catch let error as NSError {
print("request error: \(error)")
}
}
}