Ios 为什么不';t TableView,TableCell是否出现?
我正在制作一个聊天应用程序,所以我试图制作一个显示profileimage和name的tableview,但它现在不起作用 我为此应用程序使用Firebase存储和数据库 有人能告诉我我做错了什么吗 这是存储从Firebase提取的用户信息的UserModel类Ios 为什么不';t TableView,TableCell是否出现?,ios,swift,firebase,Ios,Swift,Firebase,我正在制作一个聊天应用程序,所以我试图制作一个显示profileimage和name的tableview,但它现在不起作用 我为此应用程序使用Firebase存储和数据库 有人能告诉我我做错了什么吗 这是存储从Firebase提取的用户信息的UserModel类 import UIKit class UserModel: NSObject { @objc var userName : String! @objc var profileImageUrl : String!
import UIKit
class UserModel: NSObject {
@objc var userName : String!
@objc var profileImageUrl : String!
@objc var uid : String!
}
这是PeopleViewController,它显示人们的信息,如peopleImage、姓名
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)
}
在主故事板中
我无法直接在PeopleViewController上插入TableView
那么,为什么运行时PeopleViewController上不显示TableView?您忘记为UITableView提供框架,请尝试以下操作:
override func viewDidLoad() {
super.viewDidLoad()
tableview = UITableView(frame: view.frame) // Or your desired frame
// Rest of your code
}
我想你错过了在桌子上登记你的手机 下面是代码的解决方案
let tableView: UITableView = UITableView()
tableView.frame = CGRect(x: 10, y: 10, width: 100, height: 500)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
试试这个您的TABLEVIEW工作正常..请再次检查您的FIRBASE API
尝试将tableView的框架设置为
tableView=UITableView(框架:.0)
,并确保在约束代码之前设置了tableView.TranslatesAutoResizezingMacInToConstraints=false
。此外,tableview不是可选的,因此不需要在变量tableview
之后包含?
。您不需要这个DispatchQueue.main.async{
因为Firebase闭包中的UI调用是在主线程上执行的。同时,将变量命名为与类名相同的东西可能会使“UserModel.setValuesForKeys(fch)”非常混乱。如何命名变量“user”let user=UserModel()
。一旦更正了变量名,代码的Firebase部分实际上可以工作。您是否测试了数组是否正确填充?