Simple UITableView in Swift - unexpectedly found nil

后端 未结 13 1621
旧巷少年郎
旧巷少年郎 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:15

    You need to check two things

    1. Register cell with nib name in viewDidLoad


    func viewDidLoad() 
    {
        super.viewDidLoad()
        listTableView.register(UINib.init(nibName: "ListProductCell", bundle: nil), forCellReuseIdentifier: "ListProductCell")
     }
    

    2. Create custom cell this way.

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

提交回复
热议问题