xcode SwiftUI -自定义UITableView未加载,正在执行除cellForRow之外的委托方法

kjthegm6  于 2023-05-30  发布在  Swift
关注(0)|答案(2)|浏览(177)

我有这样的情况,我必须在SwiftUI中使用TableViewUIKit自定义视图,但是表视图没有出现在SwiftUI中,但是除了cellForRow之外,委托方法正在执行。我没有在我的项目中使用脚本。
示例

这里是我的自定义视图,带有我的tableview和delegate方法

class CustomTableView: UIView, UITableViewDelegate, UITableViewDataSource {
    var tableView: MyCustomTableView {
        let tableview = MyCustomTableView(frame: .zero, style: .plain)
        tableview.delegate = self
        tableview.dataSource = self
        return tableview
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "Hello \(indexPath.row)"
        return cell
   }
}

这是我的tableview

class MyCustomTableView: UITableView {

    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

This is my Representable for CustomTableView

struct CustomTableViewRepresentable: UIViewRepresentable {

    func makeUIView(context: Context) -> CustomTableView {
        CustomTableView(frame: .zero)
    }

    func updateUIView(_ uiView: CustomTableView, context: Context) {
        uiView.tableView.reloadData()
    }
}

这是我的SwiftUI视图,将在其中一个按钮单击事件上加载

struct SwiftUIView: View {

    var body: some View {
        VStack {
            CustomTableViewRepresentable()
        }.background(Color.yellow)
    }
}

请帮助和建议。谢谢。
我在细节部分添加了代码,我尝试过,但我没有运气。我错过了一些东西,但不知道是什么?
以下是示例项目的链接:https://www.dropbox.com/s/q0nfphass8tohhb/TableViewPOC.zip?dl=0

8ljdwjyq

8ljdwjyq1#

您既没有设置MyCustomTableView的框架,也没有设置Autolayout约束。
请给予它足够的布局数据,以便它可以成功地呈现。

abithluo

abithluo2#

您需要注册一个Cell,因此需要调用register(_:forReuseIdentifier:),如下所示:

var tableView: MyCustomTableView {
  let tableview = MyCustomTableView(frame: .zero, style: .plain)
  tableview.delegate = self
  tableview.dataSource = self
  tableView.tableView.register(UITableViewCell.self,
                               forCellReuseIdentifier: "DefaultCell")
  return tableview
}

因此,请使用以下选项之一:
1.默认值:tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
1.定制:tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "DefaultCell")
1.从笔尖:tableView.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "DefaultCell")

**稍后在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)中,您需要使用dequeueReusableCell(withIdentifier:)**操作注册的单元格,如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  guard let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultCell")
  else { return }

  // Cell customizations...

  return cell
}

相关问题