Similar question is asked here but that doesn't solve this problem for me. I've added a tableView
in a ViewController
. Made extended the class with it's dataSource and Delegate and added the required methods for it.
Then I made a prototype cell in this table (Not the separate .xib for it) and made a class for that TableViewCell
and collected the @IBOutlet
:
@IBOutlet weak var titleOfAccount: UILabel!
@IBOutlet weak var lastModified: UILabel!
@IBOutlet weak var accountImage: UIImageView!
@IBOutlet weak var cellView: UIView!
Then in cellForRowAt
method of table view when I wrote
cell.titleOfAccount.text = "Hello World"
cell.lastModified.text = "Last Modified: 21 May 2017"
and ran the code it crashed with this error.
fatal error: unexpectedly found nil while unwrapping an Optional value
What I did after searching here and there I went back to tableViewCell
class and changed !
to ?
and updated the code in cellForRowAt
as:
cell.titleOfAccount?.text = "Hello World"
cell.lastModified?.text = "Last Modified: 21 May 2017"
Now when I ran the program it runs successfully but there is no cell appearing in the tableView
and I've also implemented the method:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3;
}
Here is the full code of my cellForRowAt
method:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: AccountsTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! AccountsTableViewCell
cell.backgroundColor = UIColor.clear
cell.clipsToBounds = true
cell.cellView.layer.shadowColor = UIColor.black.cgColor
cell.cellView.layer.shadowOpacity = 1
cell.cellView.layer.shadowOffset = CGSize.zero
cell.cellView.layer.shadowRadius = 10
cell.titleOfAccount.text = "Hello World"
cell.lastModified.text = "Last Modified: 21 May 2017"
cell.accountImage.image = UIImage(named: "fb")
return cell
}
P.S. I've also added self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
in viewDidLoad
I think you can resolve this by modify the code in the ViewController.
Use below
self.tableView.register(UINib(nibName: "AccountsTableViewCell", bundle: nil), forCellReuseIdentifier: cellReuseIdentifier)
instead of
self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With