gpt4 book ai didi

ios - 未调用 cellForRowAt 但正在调用 numberOfSection 和 numberOfRowsInSection

转载 作者:可可西里 更新时间:2023-11-01 00:56:08 25 4
gpt4 key购买 nike

我的代码:

//Adding Destination VC as subview in my current view's view container
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewControllerID")
containerView.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)

//Now implementing Table view in the destination vc

class DestinationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableView = UITableView()
var tableData = ["Beach", "Clubs", "Chill", "Dance"]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let screenBounds = UIScreen.main.bounds
let rect = CGRect(x: 0, y: 0, width: screenBounds.width, height: screenBounds.height)
tableView = UITableView(frame: rect, style: UITableViewStyle.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.blue

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "my")

view.addSubview(tableView)

tableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath)
cell.textLabel?.text = "This is row \(tableData[indexPath.row])"

return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4//tableData.count
}
}

如果我将目标 viewController 设置为 Storyboard中的初始 vc,那么一切正常。但是如果在容器 View 中添加为 subview ,则不会调用 cellForRowAt 方法,但会调用 numberOfSection 和 numberOfRowsInSection。

有没有人有什么解决办法?

最佳答案

在 subview 之前添加self.addChildViewController(viewController)

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewControllerID")

self.addChildViewController(viewController)
containerView.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)

关于ios - 未调用 cellForRowAt 但正在调用 numberOfSection 和 numberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47976897/

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