-4

I am using tableview with custom cell in swift 3.0. I take a textview and give outlet of class of tabelview cell and accessing in tableview delegate method but its showing me error like unexpectedly found nil while unwrapping an Optional value

here is my code. Record is array

Chek this Screenshot. FYI I have taken A new custom class and Give Outlet to it but same problem ocuured

Screenshot

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



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
   let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

  if let str = record.object(at: indexPath.row) as? String {
        print(cell.txtview)

            cell.txtview.text = str  

    }


    return cell
}

Its perfectly working in Static cell but in custom cell it showing me error.

cell.textview is not taken from custom cell

image

Please help me with it

Jitendra Modi
  • 2,344
  • 12
  • 34

1 Answers1

-1

Please try with following line not sure about that:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
         let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

        if let title = record.object(at: indexPath.row) as? String {
                cell.textview.text = title
        } else {
                cell.textview.text = ""
        }

        return cell
}
Mehul Sojitra
  • 1,181
  • 9
  • 15