ios - 当 UITableViewController 不是类类型时,如何快速获取 self.tableView.reload Table()

标签 ios swift uitableview

我有一个普通的 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/

相关文章:

android - 适用于 Android 和 iOS 的跨平台开发

ios - 使用未声明的类型 'UNUserNotificationCenter'

ios - 警告 : Attempt to present View Controller on * which is already presenting <UISearchController: 0x142a1f7c0>

ios - 自定义单元格图像在 UITableView 中自行更改

javascript - iPhone 中的调用自动化

iphone - 如何支持多个 iOS 版本?

ios - 在 Swift 字符串中操作标记

swift - 如何在 swift 中获取命名空间、行、文件、列、函数和 dso 句柄等调试标识符?

ios - 将 AutoLayout 约束绑定(bind)到自定义单元格中的 UITableView 分隔符插入?

ios - 按下主页按钮后,如何重新加载Web View ?