Since UITableView is in UIViewController, I made a class that conforms UITableViewDataSource protocol and attaches it to the table view in UIViewController's viewDidLoad() like below.
delegate for table view in UIViewTableController:
class TableViewController: NSObject, UITableViewDataSource, UITableViewDelegate {
var data
// where should I do this task and how can i reload data after task?
data = asyncFuncToGetData()
// this is not working
override init() {
data = asyncFuncToGetData()
}
func tableView() {
// uses variable data
}
}
UIViewController:
class ContainerViewController: UIViewController {
@IBOutlet weak var mainTableView: UITableView!
let delegate = TableViewController()
override func viewDidLoad() {
super.viewDidLoad()
mainTableView.dataSource = delegate
mainTableView.delegate = delegate
}
}
I have to fetch data from server because table needs it. So where should I do that? This can be done easily when UITableViewController conforms UITableViewDataSource protocol, but I do not want that way.