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

时间:2017-03-22 11:50:47

标签: ios swift xcode uitableview

我有ViewController,其中我有UITableview和自定义单元格。我的代码是正确的,我也委托和数据源。我在tableViewCell中也有IBoutlet。 我只会看到空行,我没有看到在故事板中设置的cellbackground颜色。

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()

        }
    }

}

}

3 个答案:

答案 0 :(得分:0)

我认为你应该为row指定rowheight。

ex.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0;
}

大部分都解决了你的问题。让我知道如果不解决。

由于 Nirav Zalavadia

答案 1 :(得分:0)

我有两个问题要问你:

  1. 你可以硬编码aray并检查吗?这将消除服务器问题。
  2. todaysPatientsListCell是否已分配给Storyboard中的自定义单元格?
  3. 让我知道他们是否有所帮助。

答案 2 :(得分:-1)

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

self.tableView.reloadData()

以下内容:

DispatchQueue.main.async {
    self.tableView.reloadData()
}
相关问题