使用自定义视图单元格时的TableViewCell不显示

发布于 2025-01-24 06:23:12 字数 2316 浏览 1 评论 0原文

我有一个桌面视图,可以实现我在应用程序中制作的自定义单元格。表视图从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

enter image description here

Sorry I dont have enough reps yet

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱人配狗天长地久 2025-01-31 06:23:12

确保在接口构建器中的原型单元格上设置单元格标识符属性。另外,您可以在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 the tableView: cellForRowAt method to check if a cell is dequeued.

夏尔 2025-01-31 06:23:12

您是否注册了您的牢房?我可以看到您已经设置了委托,但没有注册单元。如果您使用的是自定义单元格,则必须注册自定义单元格。

如果您已经编程创建了TableView,则如下注册单元格:

tableView.register(YourCustomCellName.self, forCellReuseIdentifier: CellId)

或者如果您已经在故事板和故事板或XIB寄存器单元格中创建了TableView,则如下:

partnersListTableView.register(UINib(nibName: "YourCustomCellName", bundle: nil), forCellReuseIdentifier: CellId)

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:

tableView.register(YourCustomCellName.self, forCellReuseIdentifier: CellId)

or if you have created tableview in storyboard and cell in storyboard or xib register cell like this:

partnersListTableView.register(UINib(nibName: "YourCustomCellName", bundle: nil), forCellReuseIdentifier: CellId)
黄昏下泛黄的笔记 2025-01-31 06:23:12

您在方法中犯了一个错误loadItemsdata()
有完整的块可以将其恢复。您仅在错误情况下返回此完成,但在成功的情况下不会返回。因此,您也必须在这里退还。

func loadItemsData(completed: @escaping (_ dataGet: Bool) -> void) {
    db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
        guard Error == nil else {
            print("Error getting data")
            completed(false)
            return
        }
        
        for document in QuerySnapshot!.documents {
            print(document.data())
            let item = Item.init(document.data())
            self.Items.append(item)
            completed(true)
        }
    }
}

使用ViewDidappear()设置的ViewWillAppear()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //need to chheck if there are any documents here...
    VendorItems.loadItemsData { [weak self] dataGet in
        if dataGet {
            self?.table.reloadData()
        } else {
            print("Something went wrong")
        }
        
    }
}

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.

func loadItemsData(completed: @escaping (_ dataGet: Bool) -> void) {
    db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
        guard Error == nil else {
            print("Error getting data")
            completed(false)
            return
        }
        
        for document in QuerySnapshot!.documents {
            print(document.data())
            let item = Item.init(document.data())
            self.Items.append(item)
            completed(true)
        }
    }
}

Use viewWillAppear() insted of viewDidAppear()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //need to chheck if there are any documents here...
    VendorItems.loadItemsData { [weak self] dataGet in
        if dataGet {
            self?.table.reloadData()
        } else {
            print("Something went wrong")
        }
        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文