我认为错误源于其他地方,而不是AppDelegate。 因此,在viewcontroller类型的文件(类主页)中,我想从firebase中获取信息并进行显示。完成该部分代码后,App委托给出错误。
我已经从AppDelegate中删除了UITableViewDataSource,然后它运行了,但是没有显示Firebase信息。
class homepage: UITableViewController, CLLocationManagerDelegate{
var people = [Userx]()
@IBOutlet weak var table: UITableView!
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as! ViewControllerTableViewCell
let person: Userx = people[indexPath.row]
cell.lblName.text = person.Education
cell.lblgenre.text = person.WhatIamConsideringBuying
return cell
}
var locationManager = CLLocationManager()
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(signOut))
super.viewDidLoad()
let databaseRef = Database.database().reference()
databaseRef.child("Education").observe(DataEventType.value, with: {snapshot in
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
let peopleObject = people.value as? [String: AnyObject]
let peopleEducation = peopleObject?["Education"]
let peopleWhatIamConsideringBuying = peopleObject?["WhatIamConsideringBuying"]
let peoplePhotoPosts = peopleObject?["PhotoPosts"]
let people = Userx(Education: peopleEducation as! String?, WhatIamConsideringBuying: peopleWhatIamConsideringBuying as! String?, PhotoPosts: peoplePhotoPosts as AnyObject)
self.people.append(people)
}
self.table.reloadData()
}
})
///this is different file code for class ViewControllerTableViewCell
class ViewControllerTableViewCell: UITableViewCell {
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblgenre: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
由于错误可能在情节提要中,所以主页的路径为:主页场景-主页-表-单元格-内容视图-标签1和标签2。来自表的数据源和代理通过插座连接到主页。
我只希望AppDelegate中的错误消失,以便可以从Firebase中获取数据。
这是应用程序委托代码:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
答案 0 :(得分:-1)
在您的viewDidLoad()方法中编写此代码
table.dataSource = self
table.delegate = self
并在cellForRowAt中更新代码
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
if let person = people[indexPath.row] {
cell.lblName.text = person.Education
cell.lblgenre.text = person.WhatIamConsideringBuying
}
return cell
}
防止应用崩溃
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(signOut))
super.viewDidLoad()
let databaseRef = Database.database().reference()
databaseRef.child("Education").observe(DataEventType.value, with: {snapshot in
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
let peopleObject = people.value as? [String: AnyObject]
let peopleEducation = peopleObject?["Education"]
let peopleWhatIamConsideringBuying = peopleObject?["WhatIamConsideringBuying"]
let peoplePhotoPosts = peopleObject?["PhotoPosts"]
if let peopleEducation = peopleEducation as? String {
if let whatIAmConsideringBuying = peopleWhatIamConsideringBuying as? String? {
if let photoPosts = photoPost as? AnyObject {
let people = Userx(Education: peopleEducation, WhatIamConsideringBuying: whatIAmConsideringBuying, PhotoPosts: photoPosts)
self.people.append(people)
}
}
}
}
self.table.reloadData()
}
})