Pretty simple code:
func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
func tableView(tableView:UITableView!, numberOfRows
You need to check two things
func viewDidLoad()
{
super.viewDidLoad()
listTableView.register(UINib.init(nibName: "ListProductCell", bundle: nil), forCellReuseIdentifier: "ListProductCell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListProductCell") as! ListProductCell
return cell
}
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")
}
Possibly that your view in Main.Storyboard lost its IBOutlet reference in ViewController file, just link it again.
Try doing this:
let cell = tableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookTableViewCell
and don't forget to set the reuse identifier in your storyboard
I was getting this error because I didn't have the identifier written in the Storyboard of the Custom Cell.
Also make sure it matches you code in:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableCell") as CustomTableCell
...
}
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)