gpt4 book ai didi

ios - 在 cellForRowAt 中无法使用 UITableView 但在 Swift 中可以使用 numberOfRowsInsection

转载 作者:搜寻专家 更新时间:2023-11-01 06:55:55 25 4
gpt4 key购买 nike

我写了一个函数来在 UITableView 中添加一个 Cell。

numberOfRowsInSection 有效,但 cellForRowAt 无效。

class ShopMenuViewCell: UITableViewCell {
@IBOutlet weak var menuTableView: UITableView!

var menuPrice: MenuPrice!

override func awakeFromNib()
{
super.awakeFromNib()
}
}

extension ShopMenuViewCell
{
func setView()
{
self.menuTableView.delegate = self
self.menuTableView.dataSource = self
self.menuTableView.regCells(cells: ["ShopMenuInformationCell"])

DispatchQueue.main.async
{
self.menuTableView.reloadData()
}
}
}

extension ShopMenuViewCell : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ShopMenuInformationCell", for: indexPath) as? ShopMenuInformationCell,
let menu = self.menuPrice,
let name = menu.name,
let price = menu.amt else { return ShopMenuInformationCell() }

return cell
}
}

在 ShpMenuInformationCell.swift 中

class ShopMenuInformationCell: UITableViewCell
{
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var shopNameLabel: UILabel!
@IBOutlet weak var menuNameLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!

override func awakeFromNib()
{
super.awakeFromNib()
}
}

我看到了添加一个 ViewController 的答案,但我不知道该怎么做,因为它不是 Storyboard。 (.xib)

我想是因为我在UITableViewCell类中添加了UITableViewCell

你知道为什么吗?

最佳答案

我遇到了大约 5 天的问题并找到了解决方案!

第一个引用站点:Joel 的回答 cellForRowAtIndexPath: not called

第二个引用站点:Paolo 的回答 Swift: UITableViewCell rowheight

当我添加另一种 TableCell 时,相关单元格的垂直尺寸很小。

所以我在重新加载数据之前增加了Cell的RowHeight(frame.height)的大小并且它是固定的!

目前,cellforrowat 安全运行。您的回答对结果有帮助。

关于ios - 在 cellForRowAt 中无法使用 UITableView 但在 Swift 中可以使用 numberOfRowsInsection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53371245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com