簡體   English   中英

未調用 cellForRowAt 但正在調用 numberOfSection 和 numberOfRowsInSection

[英]cellForRowAt not being called But numberOfSection and numberOfRowsInSection is being called

我的代碼:

//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,那么一切正常。但是如果在容器視圖中添加為子視圖,則不會調用 cellForRowAt 方法,而是調用 numberOfSection 和 numberOfRowsInSection。

有沒有人有任何解決方案?

子查看前添加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)

確保在UITableView上設置了約束。 如果是這樣,請嘗試手動設置UITableViewCell高度。

首先,您需要在故事板中有一個 tableview。 創建 tableView 后,您可以通過以下方式以編程方式對其進行子查看:

self.view.addSubview(tableView)

或者在你的故事板中添加 tableView 並像這樣創建一個 IBOutlet:

@IBOutlet weak var tableView: UITableView!

其次,您還沒有初始化您的單元格,您只是將先前創建的單元格出列(在您的情況下從未創建過該單元格)。 你的代碼應該是這樣的:

var cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath) as UITableViewCell?
if cell == nil { 
  cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "my")
}

檢查表格的上下左右常量。

重新檢查是否已注冊 reusableidentifier

tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM