How can I programmatically set dataSource of UITableView?

前端 未结 1 637
孤城傲影
孤城傲影 2020-12-14 13:50

I am having a strange problem. I am trying to assign a dataSource to a table programatically.

I have created a UITableView and an IBOutlet for it in my

相关标签:
1条回答
  • 2020-12-14 14:09

    Change your code to:

    class ViewController: UIViewController 
    {
        @IBOutlet weak var table: UITableView!
        var dataSource: MyData?
    
        override func viewDidLoad() 
        {
            super.viewDidLoad()
    
            dataSource = MyData()
            table.dataSource = dataSource!
        }
    }
    

    Your app breaks because the ds is deallocated as soon as viewDidLoad returns. You have to keep a reference to your data source.

    0 讨论(0)
提交回复
热议问题