【发布时间】:2017-06-05 10:04:56
【问题描述】:
当我转到 HomeViewController 时,屏幕全黑。我实际上想获取用户将它们放入 TableViewCells。我使用 Firebase 作为数据库,使用 Swift 作为语言。帮我完成这个。
import UIKit
import Firebase
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var users = [User]()
override func viewDidLoad() {
super.viewDidLoad()
fetchUser()
}
func fetchUser() {
FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User(dictionary: dictionary)
self.users.append(user)
//this will crash because of background thread, so lets use dispatch_async to fix
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}, withCancel: nil)
}
func handleCancel() {
dismiss(animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell", for: indexPath)
let user = users[indexPath.row]
cell.textLabel?.text = user.name
cell.detailTextLabel?.text = user.email
return cell
}
}
class UserCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
通过 LoginButton 下的 SWRevealViewController 呈现 Home View Controller
let vc : SWRevealViewController = self.storyboard?.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
self.present(vc, animated: true, completion: nil)
【问题讨论】:
-
能否请您展示一下您如何呈现
Home View Controller的代码? PS:顺便说一句,您在这里有两个问题:1.为什么显示黑屏2.如何获取数据 -
@TungFam 我已经更新了问题
标签: ios swift uitableview firebase firebase-realtime-database