[英]uiTableview cell is not displaying in view controller
I have ViewController in which I have UITableview and custom cell. 我有ViewController,其中有UITableview和自定义单元格。 My code is correct and I did delegate and datasource also.
我的代码是正确的,我也做了委托和数据源。 I have IBoutlets in tableViewCell also.
我在tableViewCell中也有IBoutlets。 I will see only empty lines nothing else, I don't see cellbackground colour also which is set in storyboard.
我将只看到空行,没有看到也在情节提要中设置的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()
}
}
}
} }
I think you should specify rowheight for row. 我认为您应该为row指定rowheight。
ex.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0;
}
Mostly that resolved your problem.Let me know if not resolve. 通常可以解决您的问题。如果无法解决,请通知我。
Thanks Nirav Zalavadia 感谢Nirav Zalavadia
I have two questions for you: 我有两个问题要问你:
Let me know if they helped. 让我知道他们是否有帮助。
When you reload data after fetching the data, you need to do that in the main thread. 在获取数据后重新加载数据时,需要在主线程中执行此操作。 So replace this line:
因此,请替换此行:
self.tableView.reloadData()
With the following: 具有以下内容:
DispatchQueue.main.async {
self.tableView.reloadData()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.