-1

I am fairly new to iOS and I am trying to make a table view controller. When coding, I get the error message in my title. It is obvious that I am doing something wrong. Can someone please explain to me what I am doing wrong. I will leave my code below.

Please note that I am not using a storyboard.

class settingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let elements = ["horse", "cat", "dog", "potato","horse", "cat", "dog", "potato","horse", "cat", "dog", "potato"]

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        // Error is on the following line
        tableView.delegate = self
        tableView.dataSource = self

        super.viewDidLoad()
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elements.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

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

        cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2

        cell.animalLbl.text = elements[indexPath.row]
        cell.animalImage.image = UIImage(named: elements[indexPath.row])
        cell.animalImage.layer.cornerRadius = cell.animalImage.frame.height / 2

        return cell
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Either you didn't hook up the interface builder outlet for `tableView`, or there is no reusable cell with identifier `customCell`, or there is such a cell, but it's not of type `CustomTableViewCell`. – Alexander Jun 29 '18 at 18:12
  • Where the the crash points to (the line) – Shehata Gamal Jun 29 '18 at 18:14
  • Adding to what @Alexander was saying, to make sure `customCell` is an identifier, by clicking on your cell in the Storyboard and set the `identifier` in the Attributes inspector. – George Jun 29 '18 at 18:15
  • @Alexander I am coding programmatically. not with storyboards. –  Jun 29 '18 at 18:20
  • @Sh_Khan the error falls under tableView.delegate = self tableView.dataSource = self –  Jun 29 '18 at 18:20
  • you need to **tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")** in **viewDidLoad** – Shehata Gamal Jun 29 '18 at 18:21
  • 1
    Also if it's programmatically then why you set **@IBOutlet** , i think the table itself must be initiated – Shehata Gamal Jun 29 '18 at 18:26
  • @Sh_Khan thank you for taking the time to help me solve this problem. unfortunately, I am still receiving the same problem. –  Jun 29 '18 at 18:29

2 Answers2

1

You state that you are not using a storyboard yet you have declared your tableView property as an outlet. Don't do that. Outlets are only for storyboards.

You never actually create an instance of a UITableView and assign it to your tableView property. This is why you get the crash when attempting to access an implicitly unwrapped optional that is nil.

You also have a UIViewController. Why not use a UITableViewController and save a lot of work?

You also need to register your custom cell class.

Also note that it is standard to start class, struct, and enum names with uppercase letters. Variable, function, and case names start with lowercase letters.

Your code should be:

class SettingsViewController: UITableViewController {
    let elements = ["horse", "cat", "dog", "potato","horse", "cat", "dog", "potato","horse", "cat", "dog", "potato"]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elements.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell

        cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2

        cell.animalLbl.text = elements[indexPath.row]
        cell.animalImage.image = UIImage(named: elements[indexPath.row])
        cell.animalImage.layer.cornerRadius = cell.animalImage.frame.height / 2

        return cell
    }
}

One other thing to consider. Since you are making all of your rows the same height, it would be more efficient to remove the implementation of the heightForRowAt method and add the following line to viewDidLoad:

tableView.rowHeight = 100
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • thank you for taking the time out of your day to help answer my question. although I am still receiving the same error code just on a different line. cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2 the error is now falling under this line. I know you've helped fix my previous error, However the same error is falling here –  Jun 29 '18 at 19:09
  • it's the same error as my question. Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value –  Jun 29 '18 at 19:10
  • But on which line exactly? – rmaddy Jun 29 '18 at 19:11
  • cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2 –  Jun 29 '18 at 19:12
  • OK, that's a whole new issue with your custom cell class. Perhaps you have a similar problem. Did you declare `cellView` as an outlet? Don't use outlets unless you are using a storyboard. And there is a HUGE difference between declaring a variable and actually creating an instance of a class. – rmaddy Jun 29 '18 at 19:13
  • I’m sorry to ask for more of your time, but could you show me how to call it? –  Jun 29 '18 at 19:25
  • That's a whole new question independent of this question. Mark this question as solved since my answer fixes your original issue. Post a new question with details specific to your new issue. Include relevant code (including your custom cell class). – rmaddy Jun 29 '18 at 20:10
  • I came across the OP’s new question on this topic. Here’s a link if anyone trying to find it: https://stackoverflow.com/q/51109288/8289095 – Chris Jun 29 '18 at 21:10
0

The code you given is working fine when i commented these lines

//  cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2

What is cellview ?? where you created reference ?

chandra1234
  • 357
  • 6
  • 15
  • This is a comment, not an answer. Please do not post comments and answers. Wait until you have enough rep to post comments. – rmaddy Jun 29 '18 at 20:08