【问题标题】:No Data Fetched From Firebase in Cell In UiTableViewCell Swift 3没有从 UiTableViewCell Swift 3 中的单元格中的 Firebase 获取数据
【发布时间】:2017-04-06 13:11:54
【问题描述】:

大家好,我遇到了一个问题,无法在任何地方找到解决方案。谁能告诉我这是什么问题,我刚刚开始快速学习我想从我的 firebase 数据库中获取数据并将其打印在 uitableviewcell 上

我创建了一个 swift 文件,其中包含

类分类:NSObject { var 类别:字符串? }

我的视图控制器在下面

import UIKit
import FirebaseDatabase


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var categoryList = [Categories]()


    var refHandle:UInt!
    var ref:FIRDatabaseReference?

    @IBOutlet weak var listOfCategories: UITableView!
    @IBOutlet weak var menuBtn: UIBarButtonItem!


    override func viewDidLoad() {
        super.viewDidLoad()

        ref = FIRDatabase.database().reference()

        menuBtn.target = revealViewController()
        menuBtn.action = #selector(SWRevealViewController.revealToggle(_:))

        fetchCategories()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return categoryList.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")

        // Set cell content
        cell.textLabel?.text = categoryList[indexPath.row].categories

    return cell


    }

    func fetchCategories(){

        refHandle = ref?.child("Categories").observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {

                print(dictionary)

                let categories = Categories()



                categories.setValuesForKeys(dictionary)
                self.categoryList.append(categories)

                DispatchQueue.main.async
                    {
                   self.listOfCategories.reloadData()
                }

            }
        })

    }


}

我的数据库

-Categories
  -name
    Bikes:"Bikes"
    Cars:"Cars"
    Cats:"Cats" 

如果我打印(categorList)我也没有在控制台上得到它。下面是日志

2017-04-06 06:09:50.531 壁纸应用[1716] [Firebase/Analytics][I-ACS023007] Firebase Analytics v.3700000 已启动 2017-04-06 06:09:50.534 壁纸应用[1716] [Firebase/Analytics][I-ACS023008] 要启用调试日志记录,请设置 以下应用程序参数:-FIRAnalyticsDebugEnabled(请参阅 http://goo 。 gl/RfcP7r) 2017-04-06 06:09:50.565 壁纸应用[1716] [Firebase/Analytics][I-ACS003007] 成功创建 Firebase Analytics 应用程序自动委托代理。要禁用 代理,将标志 FirebaseAppDelegateProxyEnabled 设置为 NO Info.plist 2017-04-06 06:09:50.580 壁纸应用[1716] [Firebase/Analytics][I-ACS005000] AdSupport 框架不是 当前链接。某些功能将无法正常运行。学到更多 在http://goo。 gl/9vSsPb 2017-04-06 06:09:50.589 壁纸应用[1716] [Firebase/Analytics][I-ACS023012] Firebase 分析已启用 [“汽车”:汽车,“自行车”:自行车,“猫”:猫] 2017-04-06 06:09:52.565 壁纸应用程序[1716:28745] * 由于未捕获而终止应用程序 异常“NSUnknownKeyException”,原因: '[ setValue:forUndefinedKey:]:这个类不是键值 符合关键汽车的编码。 * 第一次抛出调用堆栈:( 0 CoreFoundation 0x0000000104725d4b exceptionPreprocess + 171 1 libobjc.A.dylib
0x0000000103d6621e objc_exception_throw + 48 2 核心基础
0x0000000104725c99 -[NSException raise] + 9 3 基础
0x00000001038749df -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291 4 基础 0x00000001038d672f -[NSObject(NSKeyValueCoding) setValuesForKeysWithDictionary:] + 301 5 壁纸应用程序 0x00000001026fe886 _TFFC14Wallpapers_App14ViewController15fetchCategoriesFT_T_U_FCSo15FIRDataSnapshotT_ + 1126 6 壁纸应用程序 0x00000001026fed8c _TTRXFo_oCSo15FIRDataSnapshot__XFdCb_dS_
+ 60 7 壁纸应用程序 0x00000001027c85b0 63-[FIRDatabaseQuery observeEventType:withBlock:withCancelBlock:]_block_invoke + 37 8
壁纸应用程序 0x00000001027efeb3 __43-[FChildEventRegistration fireEvent:queue:]_block_invoke.68 + 88 9 libdispatch.dylib 0x0000000107470978 _dispatch_call_block_and_release + 12 10 libdispatch.dylib 0x000000010749a0cd _dispatch_client_callout + 8 11 libdispatch.dylib 0x000000010747a8a4 _dispatch_main_queue_callback_4CF + 406 12 核心基础 0x00000001046e9e49 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE
+ 9 13 核心基础 0x00000001046af37d __CFRunLoopRun + 2205 14 CoreFoundation 0x00000001046ae884 CFRunLoopRunSpecific + 420 15 图形服务
0x0000000109439a6f GSEventRunModal + 161 16 UIKit
0x000000010525cc68 UIApplicationMain + 159 17 壁纸应用
0x0000000102701fcf main + 111 18 libdyld.dylib
0x00000001074e668d 开始 + 1 ) libc++abi.dylib: 终止于 NSException (lldb) 类型的未捕获异常

【问题讨论】:

    标签: ios uitableview firebase swift3 firebase-realtime-database


    【解决方案1】:

    你正在使用的方法

    setValuesForKeys
    

    这个方法描述是这样说的:

    Sets properties of the receiver with values from a given dictionary, using its keys to identify the properties.
    

    您的错误表明,您的类别类不符合键“Cars”的键值,因为您的“Categories”类中没有名为“Cars”的键/属性。

    据我所知,您正试图在 UITableViewCell 中打印所有这些类别。我建议将您的 Categories 类更改为以下内容:

    struct Category() {
        var key: String
        var value: String
    }
    

    当您在 fetchCategories() 方法中获得响应时发布。解析那个字典:

    for (key, value) in dictionary {
        let category = Category(key: key, value: value as! String)
        self.listOfCategories.append(category)
    }
    

    在您的 cellForRowAtIndexPath 中。你可以这样设置文本。

    cell.textLabel?.text = categoryList[indexPath.row].value
    

    如果您只需要从 Firebase 收到的响应中的值,您可能需要简化 Category 结构。然后你的结构就变得很简单了:

    struct Category() {
        var name: String
    }
    

    我更喜欢尽可能使用结构而不是类,但如果你真的需要,你可以使用类。

    【讨论】:

      【解决方案2】:

      在我看来你有两个问题 1)在单元格中显示数据 2)打印数据以便您查看

      我将解决 2),因为我认为一旦您看到数据,您就可以使用它(至少这是我的经验 - 对 Xcode 来说也是相当新的)

      Xcode 不是“简单”地打印我们想要的数据......(您可能会发现它和我一样令人沮丧 - 来自 JS/jQuery/PHP 背景)

      我发现最简单的是使用一个名为“SimpleJSON”的插件(还有其他插件,这是我发现的适合我的目的)并将数据带到一个 JSON 包中,在那里我可以看到数据(整个想法,对吗?),然后根据需要从那里开始工作。

      在 pod 文件中,添加即可

      import SwiftyJSON
      

      然后在你想要查看数据的代码中

      let categoriesJSON = JSON(categories)
      print(categoriesJSON)
      

      从那里,您将获得一个可以在数组中使用的列表

      print(categoriesJSON["name"]["Cars"]
      

      也许不是一个完全“Swift-y”的解决方案,但它可以完成工作!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多