使用自定义视图单元格时的TableViewCell不显示
我有一个桌面视图,可以实现我在应用程序中制作的自定义单元格。表视图从Firebase获取数据,并在视图出现后重新加载。由于某种原因,表视图行不会出现。这是为什么?
这是我的代码
tableView实现方法
extension VendorItemsController:UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return VendorItems.Items.count
}
func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = table.dequeueReusableCell(withIdentifier:"cell",for: indexPath) as? ItemDisplayCell {
cell.DescriptionLabel?.text = VendorItems.Items[indexPath.row].itemDescription
cell.PriceLabel.text = VendorItems.Items[indexPath.row].price
// print(cell.PriceLabel.text)
return cell
}
return UITableViewCell()
}
我称之为委托方法并重新加载我的数据
override func viewDidLoad() {
// if(auth.currentUser != nil){Progress}
// Do any additional setup after loading the view.
//no cnnection to table view omgggg
VendorItems = Vendor()
table.delegate = self
table.dataSource = self
//table.rowHeight = UITableView.automaticDimension
}
override func viewDidAppear(_ animated: Bool) {
//need to chheck if there are any documents here...
print(VendorItems.Items.count)
VendorItems.loadItemsData {
self.table.reloadData()
}
}
这是供应商类中的loadItemsmethod,每次都在视图中运行出现
func loadItemsData(completed: @escaping () -> ()) {
db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
guard Error == nil else {
print("Error getting data")
return completed()
}
for document in QuerySnapshot!.documents {
print(document.data())
let item = Item.init(document.data())
self.Items.append(item)
}
}
}
这是我从表查看cellforrowat方法中继承的类
class ItemDisplayCell: UITableViewCell {
@IBOutlet weak var DescriptionLabel: UILabel!
@IBOutlet weak var PriceLabel: UILabel!
@IBOutlet weak var itemImage: UIImageView!
}
最后,这是我试图制作
I have a tableview that implements a custom cell that I made in my app. The table view gets its data from firebase and reloads it once the view appears. For some reason the table view rows dont appear. Why is that?
Here is my code
The tableview implementation methods
extension VendorItemsController:UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return VendorItems.Items.count
}
func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = table.dequeueReusableCell(withIdentifier:"cell",for: indexPath) as? ItemDisplayCell {
cell.DescriptionLabel?.text = VendorItems.Items[indexPath.row].itemDescription
cell.PriceLabel.text = VendorItems.Items[indexPath.row].price
// print(cell.PriceLabel.text)
return cell
}
return UITableViewCell()
}
Where I call the delegate methods and reload my data
override func viewDidLoad() {
// if(auth.currentUser != nil){Progress}
// Do any additional setup after loading the view.
//no cnnection to table view omgggg
VendorItems = Vendor()
table.delegate = self
table.dataSource = self
//table.rowHeight = UITableView.automaticDimension
}
override func viewDidAppear(_ animated: Bool) {
//need to chheck if there are any documents here...
print(VendorItems.Items.count)
VendorItems.loadItemsData {
self.table.reloadData()
}
}
This is the loadItemsMethod in the Vendor class, that is run everytime the view appears
func loadItemsData(completed: @escaping () -> ()) {
db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
guard Error == nil else {
print("Error getting data")
return completed()
}
for document in QuerySnapshot!.documents {
print(document.data())
let item = Item.init(document.data())
self.Items.append(item)
}
}
}
This is the class that I inherit from in my table view cellForRowAt method
class ItemDisplayCell: UITableViewCell {
@IBOutlet weak var DescriptionLabel: UILabel!
@IBOutlet weak var PriceLabel: UILabel!
@IBOutlet weak var itemImage: UIImageView!
}
Finally, this is the prototype cell that I am trying to make
Sorry I dont have enough reps yet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保在接口构建器中的原型单元格上设置
单元格标识符
属性。另外,您可以在tableView:cellforrowat
方法中设置一个断点,以检查单元是否被脱水。Make sure you are setting the
Cell Identifier
property on your prototype cell in Interface Builder. Also you can set a breakpoint in thetableView: cellForRowAt
method to check if a cell is dequeued.您是否注册了您的牢房?我可以看到您已经设置了委托,但没有注册单元。如果您使用的是自定义单元格,则必须注册自定义单元格。
如果您已经编程创建了TableView,则如下注册单元格:
或者如果您已经在故事板和故事板或XIB寄存器单元格中创建了TableView,则如下:
Did you register your cell or not? I can see you have set delegates but not register cell. if you're using custom cell then you have to register your custom cell.
if you have created tableview programmatically then register cell like this:
or if you have created tableview in storyboard and cell in storyboard or xib register cell like this:
您在方法中犯了一个错误loadItemsdata()
有完整的块可以将其恢复。您仅在错误情况下返回此完成,但在成功的情况下不会返回。因此,您也必须在这里退还。
使用ViewDidappear()设置的ViewWillAppear()
You are doing a mistakes in your method loadItemsData()
There is completion block to get it back. you are only returning this completion in error case but not in success case. So, you have to return it here also.
Use viewWillAppear() insted of viewDidAppear()