2016-10-19 23 views
-1

Attached screenshot for issue尝试显示表格视图。我不断收到错误:问题:无法从其数据中获取单元格

Failed to obtain a cell from its dataSource class List:UITableViewController {

var users = [user]() 


override func viewDidLoad() { 
     let databaseRef = FIRDatabase.database().reference() 
    databaseRef.child("Users").queryOrderedByKey().observe(.childAdded, with: { 
    snapshot in 
    print(snapshot) 


    let addNote = (snapshot.value as? NSDictionary)?["addNote"] as? String ?? "" 
    let uid = (snapshot.value as? NSDictionary)?["uid"] as? String ?? "" 
     self.users.append(user(addNote: addNote,uid: uid)) 
     self.tableView.reloadData() 
      print(self.users) 
}) 

    } 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     print("Test\(users.count)") 
     return users.count 
     } 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 
    let nameLbl = cell?.viewWithTag(2) as! UILabel 
    nameLbl.text = self.users[indexPath.row].addNote 
    return cell! 

    } 
+1

?你的代码现在甚至不会编译 – dan

+0

我试图从firebase检索..使用swift3 Xcode 8 ...但是当我运行这个代码有自定义单元格时它会抛出相同的错误 –

+0

你不能运行你的代码张贴,它甚至不会编译。复制并粘贴实际的'cellForRowAtIndexPath'方法。 – rmaddy

回答

-1

OK ......据我了解你有你的独立的.xib文件为您tableViewCell,而不是因为你正试图与注册您的笔尖的原型细胞在使用它之前的tableview。因此,为了正确发生,您需要确保几件事:

  1. 您已在.xib文件中为您的tableView单元格指定了一个标识符。
  2. 您可以使用该标识符来注册该单元格,例如。

    让笔尖= UINib(nibName: “MyTableViewCell”,束:无)
    self.tableView.registerNib(笔尖,forCellReuseIdentifier: “ABC”)

在这里, “ABC” 是标识符。

  • 然后,在代表的cellForRowAtIndexPath,

    让细胞= tableView.dequeueReusableCellWithIdentifier( “ABC”,forIndexPath:indexPath)作为! MyTableViewCell

  • 我认为你应该使用上述步骤来注册该笔尖,然后将其取出。它应该工作正常。

    +0

    查看问题中的代码。 OP已经在调用'register'。 – rmaddy

    +0

    在代码中,既没有注册也没有任何特定的单元格......并且即使您不想出现(由于某些原因),至少您需要创建单元类的实例以执行函数正在执行cellForRow ..... – Raghav

    +0

    谢谢你回复马迪。请查看附件中提供的截图 –

    0

    ,你将有你在哪里创建`cell`到双端队列tableviewcells

    func tableView(_ tableView: UITableView, cellForRowAt indexPath:   IndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! UITableViewCell 
        return cell 
    } 
    
    相关问题