【问题标题】:TableViewCell.xib doesn't appear on tableviewTableViewCell.xib 没有出现在 tableview 上
【发布时间】:2019-02-18 15:36:52
【问题描述】:

这是我的代码,它应该在海关 tableviewcell.xib 中显示 firebase 数据库中的用户,但是当启动应用程序时,tableview 保持为空,我真的不知道代码有什么问题或缺少什么,我想这是一个非常简单的错误,但我无法弄清楚。 提前感谢那些愿意回答的人。

import UIKit
import Firebase
class UsersTableView: UIViewController, UITableViewDelegate, UITableViewDataSource {

// Outlets.
@IBOutlet weak var tableview: UITableView!

// Var.
var user = [User]()

override func viewDidLoad() {
    super.viewDidLoad()


    retrieveUsers()

    // Do any additional setup after loading the view.
}

func retrieveUsers() {
    let ref = Database.database().reference()
    ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { DataSnapshot in

        let users = DataSnapshot.value as! [String: AnyObject]
        self.user.removeAll()
        for (_, value) in users{
            //let uid = Auth.auth().currentUser!.uid
            if let uid = value["userID"] as? String{
                if uid != Auth.auth().currentUser!.uid {
                    let userToShow = User()
                    if let fullName = value["username"] as? String , let imagePath = value["photoURL"] as? String {
                        userToShow.username = fullName
                        userToShow.imagePath = imagePath
                        userToShow.userID = uid
                        self.user.append(userToShow)

                    }
                }
            }
        }
        self.tableview.reloadData()
    })
    ref.removeAllObservers()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return  1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableview.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserTableViewCell

    cell.nameLabel.text = self.user[indexPath.row].username
    cell.userID = self.user[indexPath.row].userID
    cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)
    //checkFollowing(indexPath: indexPath)

    return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return user.count ?? 0
}
  }

  extension UIImageView{

func downloadImage(from imgURL: String!) {
    let url = URLRequest(url: URL(string: imgURL)!)

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in

        if error != nil{
            print(error)
            return
        }

        DispatchQueue.main.async {
            self.image = UIImage(data: data!)
        }
    }
    task.resume()
}
  }

【问题讨论】:

  • 我看到这里有两个步骤。您是否将数据发送到user?如果是,那么您可以检查您的 tableViewCell 数据
  • 我一直空着@RobertDresler
  • 我将数据提供给用户,但不是表格视图,因此表格视图可能有问题@LohithKorupolu
  • 先在viewDidLoad()注册单元格。

标签: swift uitableview tableview


【解决方案1】:

如果您没有在情节提要中为 tableView 设置委托和数据源,请以编程方式进行:

override func viewDidLoad() {
    super.viewDidLoad()

   //Add these 2 lines, might be missed.
    self.tableview.delegate = self
    self.tableview.dataSource = self

    retrieveUsers()
}

【讨论】:

  • 谢谢,这正是缺少的,我也错过了:tableview.register(UINib(nibName: "UserCardTableViewCell", bundle: nil), forCellReuseIdentifier: "UserCell")
【解决方案2】:

清单:

  1. 设置 tableView 委托和数据源

    self.tableview.delegate = self

    self.tableview.dataSource = self

  2. 注册自定义tableViewCell

    让 cellNIb = UINib.init(nibName:"Identifier_Name", bundle: nil) register(cellNIb, forCellReuseIdentifier: 标识符)

  3. 检查func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int的返回计数 ​

【讨论】:

    【解决方案3】:

    你忘记了两件事:

    • 将表视图的dataSource 设置为控制器。如果您不这样做,您的tableView 将不需要任何数据。所以设置在控制器的viewDidLoad

      tableview.dataSource = self
      
    • 如果您为单元格创建了自定义 xib,请不要忘记为表格视图注册您的自定义单元格(也可以在 viewDidLoad 中进行)

      tableview.register(UINib(nibName: "UserCardTableViewCell", bundle: nil), forCellReuseIdentifier: "UserCell")
      

    如果您也需要didSelectRowAt 之类的UITableViewDelegate 方法,请不要忘记将表格视图的delegate 也设置为您的控制器

    tableview.delegate = self
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 2015-08-04
      • 2016-12-14
      • 2018-01-08
      • 2021-02-02
      • 1970-01-01
      相关资源
      最近更新 更多