[英]problem displaying cells in the tableview - Swift
我正在嘗試在從信息導入到 firebase 的單元格標簽中顯示文本。 我目前有一個問題,因為單元格已形成,但我沒有看到標簽中的文本信息,如圖所示。 你能告訴我為什么我看不到信息以及如何解決這個問題。 謝謝。
struct List {
var heure: String?
var date: String?
var location: String?
var event: String?
var title: String?
var comment: String?
init(heure: String?, date: String?, location: String?, event: String?, title: String?, comment: String?) {
self.heure = heure
self.date = date
self.location = location
self.event = event
self.title = title
self.comment = comment
}
}
類第四視圖控制器:UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var Button: UIButton!
@IBOutlet weak var TableView: UITableView!
var activityList = [List]()
var ref : DatabaseReference!
let userID = Auth.auth().currentUser?.uid
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 174
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return activityList.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(identifier: "PassViewController") as? PassViewController {
vc.Heure?.text = activityList[indexPath.row].heure
vc.Date?.text = activityList[indexPath.row].date
vc.Location?.text = activityList[indexPath.row].location
vc.Titre?.text = activityList[indexPath.row].title
vc.Event?.text = activityList[indexPath.row].event
vc.Comment?.text = activityList[indexPath.row].comment
self.navigationController?.pushViewController(vc, animated: true)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ActivityViewCell
let test = activityList[indexPath.row]
cell.Titre?.text = test.title
cell.Heure?.text = test.heure
cell.Date?.text = test.date
cell.Location?.text = test.location
cell.EventType?.text = test.event
cell.comment?.text = test.comment
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
TableView.register(ActivityViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
ref = Database.database().reference()
TableView.dataSource = self
TableView.delegate = self
fetchActivityList()
}
func fetchActivityList() {
ref = Database.database().reference()
ref.child("Activities").child(userID!).observe(.childAdded, with: { (snapshot) in
let results = snapshot.value as? [String : AnyObject]
let titre = results?["Title"]
let heure = results?["Heure"]
let date = results?["Date"]
let location = results?["Location"]
let event = results?["EventType"]
let comment = results?["Comment"]
let myAct = List(heure: heure as! String?, date: date as! String?, location: location as! String?, event: event as! String?, title: titre as! String?, comment: comment as! String?)
self.activityList.append(myAct)
DispatchQueue.main.async {
self.TableView!.reloadData()
}
})
}
}
首先,您應該仔細調試是否成功獲取了您的活動列表。 在fetchActivityList()
設置斷點並打印結果。 其次,如何實例化單元格很重要:在 InterfaceBuilder 中或直接在代碼中。 您必須根據實例化類型注冊一個單元格。
@available(iOS 5.0, *)
open func register(_ nib: UINib?, forCellReuseIdentifier identifier: String)
@available(iOS 6.0, *)
open func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)
此外,在注冊單元格的類時,您可以嘗試使用ActivityViewCell.self
而不是ActivityViewCell.classForCoder()
此外,如果您嘗試將斷點設置為
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
和
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
一步一步弄清楚發生了什么。
我相信如果您仔細查看一些 codeStyle 規則並嘗試在將來保留它們會很棒。 例如,您可以使用 Ray Wenderlich 代碼樣式規則: https ://github.com/raywenderlich/swift-style-guide
作為另一個建議,嘗試獲取有關架構模式(例如 MV 模式)的更多信息。 這是使您的代碼更清晰和對象關系更直觀的方法。 我希望你能弄清楚並解決你的問題! 祝你今天過得愉快!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.