Simple UITableView in Swift - unexpectedly found nil

后端 未结 13 1505
旧巷少年郎
旧巷少年郎 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
        }
    
    0 讨论(0)
  • 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")
    }
    
    0 讨论(0)
  • 2020-12-06 01:20

    Possibly that your view in Main.Storyboard lost its IBOutlet reference in ViewController file, just link it again.

    0 讨论(0)
  • 2020-12-06 01:21

    Try doing this:

    let cell = tableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookTableViewCell
    

    and don't forget to set the reuse identifier in your storyboard

    0 讨论(0)
  • 2020-12-06 01:22

    I was getting this error because I didn't have the identifier written in the Storyboard of the Custom Cell.

    StoryBoard

    Also make sure it matches you code in:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell
    
            ...
        }
    
    0 讨论(0)
  • 2020-12-06 01:25

    Swift 5 In same storyboard two class are there Class A and Class B, Class B contains tableview outlet, when i tried to push Class A to Class B it's crashed and show tableView outlet nil.

    In class A i did navigation like below code.

    let classBObj = ClassB()
    self.navigationController?.pushViewController(classBObj, animated: true)
    

    Then i realised my mistake and used below code and it's work perfectly.

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let classBObj = storyboard.instantiateViewController(withIdentifier: "ClassB") as! ClassB
    self.navigationController?.pushViewController(classBObj, animated: true)
    
    0 讨论(0)
提交回复
热议问题