Simple UITableView in Swift - unexpectedly found nil

后端 未结 13 1629
旧巷少年郎
旧巷少年郎 2020-12-06 00:38

Pretty simple code:

func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
    return 1
}

func tableView(tableView:UITableView!, numberOfRows         


        
13条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 01:17

    In Swift 5

    First I tried registering my UITableViewCell in viewdidload() using class and identifier which I have mentioned below but it did not work for me.

    self.tableView.registerClass(MyCustomTableViewCell.self, forCellReuseIdentifier: "customCell")
    

    Solution

    Then I registered my UITableViewCell using Nib name and it worked for me
    

    Register your cell in viewdidload() using Nib name

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        //register your table view cell in Viewdidload() using Nib Name       
        tableView.register(UINib.init(nibName: "MyCustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")
    }
    

提交回复
热议问题