我正在制作一个聊天应用程序,所以我试图制作一个显示个人资料图像和名称的表格 View ,但它现在不起作用
我为此应用使用 Firebase 存储和数据库
有人可以告诉我我在这里做错了什么吗?
这是存储从 Firebase 提取的用户信息的 UserModel 类
import UIKit
class UserModel: NSObject {
@objc var userName : String!
@objc var profileImageUrl : String!
@objc var uid : String!
}
这是 PeopleViewController,显示人员图像、姓名等信息
import UIKit
import SnapKit
import Firebase
import FirebaseDatabase
import FirebaseAuth
import FirebaseStorage
class PeopleViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var array : [UserModel] = []
var tableview : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableview = UITableView()
tableview?.delegate = self
tableview?.dataSource = self
tableview?.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
view.addSubview(tableview!)
tableview?.snp.makeConstraints { (m) in
m.top.equalTo(view)
m.bottom.left.right.equalTo(view)
}
Database.database().reference().child("users").observe(DataEventType.value, with: { (snapshot) in
self.array.removeAll()
for child in snapshot.children{
let fchild = child as! DataSnapshot
let userModel = UserModel()
UserModel.setValuesForKeys(fchild.value as! [String : Any])
self.array.append(userModel)
}
DispatchQueue.main.async {
self.tableview?.reloadData()
}
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview?.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let imageview = UIImageView()
cell?.addSubview(imageview)
imageview.snp.makeConstraints { (m) in
m.centerY.equalTo(cell!)
m.left.equalTo(cell!).offset(10)
m.height.width.equalTo(50)
}
URLSession.shared.dataTask(with: URL(string: array[indexPath.row].profileImageUrl!)!){ (data, response, err) in
DispatchQueue.main.async {
imageview.image = UIImage(data: data!)
imageview.layer.cornerRadius = imageview.frame.size.width/2
imageview.clipsToBounds = true
}
}.resume()
let label = UILabel()
cell?.addSubview(label)
label.snp.makeConstraints { (m) in
m.centerY.equalTo(cell!)
m.left.equalTo(imageview.snp.right).offset(20)
}
label.text = array[indexPath.row].userName
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let view = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as? ChatViewController
view?.destinationUid = self.array[indexPath.row].uid
self.navigationController?.pushViewController(view!, animated: true)
}
在Main.storyboard中
我无法直接在 PeopleViewController 上插入 TableView
那么为什么在运行时 TableView 不会出现在 PeopleViewController 上呢?
最佳答案
您忘记提供 UITableView 的框架,请尝试以下操作:
override func viewDidLoad() {
super.viewDidLoad()
tableview = UITableView(frame: view.frame) // Or your desired frame
// Rest of your code
}
关于ios - 为什么TableView、TableCell不出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57016360/