我有一个普通的 View Controller ,里面有一个 TableView ,所以这个类只是一个普通的 UIViewController
,因此我无法用它调用 self.tableView
。
我有一个异步调用来创建一个 Aircraft 对象数组,该数组是从在线数据库中获取的,但仅在表格单元格最初加载后才完成。我正在尝试在此异步调用完成后更新表格单元格,但不确定如何操作。
我在 viewDidLaod()
中执行异步调用。这是我当前的代码,它只显示加载标签,因为尚未对飞机阵列进行更新。
class AircraftViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let log = Logger( id: String(AircraftViewController.self) )
let dvc = DownloadViewController()
var aircraftArr = [Aircraft]()
override func viewDidLoad() {
super.viewDidLoad()
dvc.getMobileSystemOverviewHTML {
dispatch_async(dispatch_get_main_queue()){
self.aircraftArr = self.dvc.aircraft
self.log.debug("\n\n\n\n\n \(self.aircraftArr) \n\n\n\n\n")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table View Delegate Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("AircraftCell", forIndexPath: indexPath) as! AircraftCell
if indexPath.row < aircraftArr.count{
let singleAircraft = aircraftArr[indexPath.row] as Aircraft
cell.aircraft = singleAircraft
} else {
cell.aircraft = Aircraft(tailID: "Loading", aircraftSN: "Loading", Box_SN: "Loading")
}
return cell
}
最佳答案
你需要像这样创建tableview的outlet
@IBOutlet var tableView: UITableView!
之后在您的 dispatch_async 中重新加载此 TableView
dvc.getMobileSystemOverviewHTML {
dispatch_async(dispatch_get_main_queue()){
self.aircraftArr = self.dvc.aircraft
self.tableView.reloadData()
self.log.debug("\n\n\n\n\n \(self.aircraftArr) \n\n\n\n\n")
}
}
希望这会有所帮助。
关于ios - 当 UITableViewController 不是类类型时,如何快速获取 self.tableView.reload Table(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37704518/