【发布时间】:2017-03-22 11:50:47
【问题描述】:
我有 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()
}
}
}
}
【问题讨论】:
-
检查您的
tableData数组是否为空。 -
不,它不是空的。它从服务器端获取值。我已经更新了我的代码,你可以检查它。
-
对我来说有一段时间发生了,我实现了协议,但忘记在故事板中引用它们。
-
检查表出口和代表是否正确连接,然后使用我回答中的给定代码。
-
您是否在调试器中检查过您的
UITableView委托方法是否被调用?如果是,numberOfRowsInSection返回时的值是多少?
标签: ios swift xcode uitableview