普通View Controller中的UITableView未使用我的自定义单元格

时间:2017-04-23 16:08:28

标签: swift xcode uitableview

我在一个普通的View Controller中放置了一个TableView,它显示了我从JSON中提取的一些简单数据。该单元格只有标签。在我的ViewController类中,我有以下代码,但我的表视图仍未显示数据:

import UIKit

class EmployeePortal: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var dateLabel: UILabel!

    var appData:Array<Dictionary<String,String>>? = nil
    var numberOfApps:Int = 0

    override func viewDidLoad() {
        self.tableView.dataSource = self
        self.tableView.delegate = self

        let currentDate = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .full
        let date = dateFormatter.string(from: currentDate)
        dateLabel.text = date

         /////Methods to get data....////

            do{
                let JSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! Array<Dictionary<String,String>>
                self.appData = JSON
                print(self.appData)  ///test just to make sure data is coming in
                self.numberOfApps = JSON.count

            //JSON file contains everything needed to make appointments
            }catch{
                print("ERROR DOWNLOADING JSON")
            }
        }
        task.resume()
      ///I think my problem might be here, not sure how to make the table 
     //actually load the data
        self.tableView.reloadData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func numberOfSections(in tableView: UITableView) -> Int {
        // There is only one section 
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //The number of rows is equal to the amount of appointments the data returned:
        return self.numberOfApps
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "appCell", for: indexPath) as! appCell  //made sure to use my custom cell
        let row = indexPath.row
        cell.patientName.text = self.appData?[row]["pName"]
        cell.reasonLabel.text = self.appData?[row]["reason"]
        cell.dateLabel.text = self.appData?[row]["date"]
        return cell //do all the stuff to update the labels and return it
    }


}

不确定我还缺少什么。我正确地设置了表视图的委托和数据源。我猜它有点小我想念但我会感激任何帮助!

1 个答案:

答案 0 :(得分:0)

DispatchQueue.main.async - 用于更新UI的原因,因为网络呼叫是后台队列。

试试这种方式

 do{
        let JSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! Array<Dictionary<String,String>>
           self.appData = JSON
           print(self.appData)  ///test just to make sure data is coming in
           self.numberOfApps = JSON.count
           DispatchQueue.main.async(execute: { () -> Void in
                 self.tableView.reloadData()  
           })
            //JSON file contains everything needed to make appointments
  }
  catch{
      print("ERROR DOWNLOADING JSON")
  }

更新: -

if let pName =  (self.appData[row]["pName"]) as? String {
   cell.patientName.text = pName
}