Ios uiTableview单元格未显示在视图控制器中

Ios uiTableview单元格未显示在视图控制器中,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我有ViewController,其中有UITableview和自定义单元格。我的代码是正确的,我也做了委托和数据源。我在tableViewCell中也有IBoutlets。 我将只看到空行,其他什么都看不到,我也看不到故事板中设置的单元格背景颜色 class HomeVC: UIViewController , UITableViewDataSource , UITableViewDelegate { @IBOutlet weak var profileImg: UIImageView!

我有ViewController,其中有UITableview和自定义单元格。我的代码是正确的,我也做了委托和数据源。我在tableViewCell中也有IBoutlets。 我将只看到空行,其他什么都看不到,我也看不到故事板中设置的单元格背景颜色

class HomeVC: UIViewController , UITableViewDataSource , UITableViewDelegate  {

@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var fullnameLbl: UILabel!
@IBOutlet weak var emailLbl: UILabel!
@IBOutlet weak var usernameLbl: UILabel!
@IBOutlet weak var editprofileBtn: UIButton!

var tableData = [AnyObject]()
var username = String()

@IBOutlet var homeSwipe: UISwipeGestureRecognizer!
@IBOutlet weak var tableView: UITableView!
var pflegerId = String()

var weekday = Date().dayOfWeek()

@IBAction func swipe(_ sender: Any) {

}

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


   //Swipe gesture

     username = (user!["username"] as AnyObject).uppercased
    let fullname = user!["fullname"] as? String
    let email = user!["email"] as? String
   pflegerId = (user!["id"] as? String)!

    usernameLbl.text = username
    fullnameLbl.text = fullname
    emailLbl.text = email

    loadData(username : username)



}

func loadData(username : String) {


    let url = URL(string: "http:..")


    var request = URLRequest(url: url!)
    request.httpMethod = "POST"
    let body = "Id=\(pflegerId)"

    request.httpBody = body.data(using: .utf8)

    URLSession.shared.dataTask(with: request) { data, response, error in

        if error == nil {

            DispatchQueue.main.async(execute: {

                do{
                    let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
                    print(json)


                    guard let parseJSON = json else {
                        print("")
                        return
                    }

                    guard let posts = parseJSON["posts"] as? [AnyObject] else{
                        print("error while parseJSON")
                        return
                    }


                    self.tableData = posts

                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }


                }catch {
                    //print("Caught an error: \(error)")
                    DispatchQueue.main.async(execute: {
                        let message = ""
                        appDelegate.infoView(message: message, color: colorSmoothRed)
                    })
                }
            })


        }else {
            //print("error: \(error)")
            DispatchQueue.main.async(execute: {
                let message = error!.localizedDescription
                appDelegate.infoView(message: message, color: colorSmoothRed)
            })

        }
        }.resume()

}





@IBAction func logout_click(_ sender: Any) {
    //alle Daten löschen
    UserDefaults.standard.removeObject(forKey: "parseJSON")
    UserDefaults.standard.synchronize()

    //jetzt wieder zur Login Ansicht wechseln.
    let loginvc = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
    self.present(loginvc, animated: true, completion: nil)


}


//TABLEVIEW

func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

//Total number of Rows in Table
func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print(tableData.count)
    return tableData.count


}





func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! todaysPatientsListCell

    // Configure the cell...
    let tableData = self.tableData[indexPath.row]
    cell.patientNameLbl?.text = tableData["name"] as? String
    cell.addressLbl?.text = tableData["address"] as? String
    cell.timeLbl?.text = tableData["timing"] as? String

    return cell

}





func coolSwiped(_ swipe : UISwipeGestureRecognizer) {

    if swipe.direction == .right {
        DispatchQueue.main.async {
            appDelegate.swipeToTomorrow()
            self.tableView.reloadData()
        }
    }
    else if swipe.direction == .left {
        DispatchQueue.main.async {
            appDelegate.swipeToYesterday()
            self.tableView.reloadData()

        }
    }

}

}

获取数据后重新加载数据时,需要在主线程中执行此操作。因此,替换这一行:

self.tableView.reloadData()
以下是:

DispatchQueue.main.async {
    self.tableView.reloadData()
}

我认为您应该为行指定行高

ex.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0;
}
基本上解决了你的问题。如果没有解决,请告诉我

谢谢
Nirav Zalavadia我有两个问题要问你:

  • 你能把aray硬编码并检查一下吗?这将消除服务器问题
  • todaysPatientsListCell是否已分配给情节提要中的自定义单元格

  • 让我知道他们是否有帮助。

    检查您的
    tableData
    数组是否为空。否,它是否为空。它从服务器端获取值。我已经更新了我的代码,你可以检查它。对我来说,有一段时间,我实现了协议,但是忘了在情节提要中引用它们。请检查表出口和委托是否正确连接,然后使用我的答案中给定的代码。您是否检查了调试器以查看是否调用了
    UITableView
    委托方法?如果是,当它返回时,
    numberOfRowsInSection
    的值是多少?代码中没有任何内容表明它是在后台队列中调用的,为什么需要这样做?该表将使用序列图像板中设置的行高。