-1

I am new to iOS and Swift 3. I had this error:

fatal error: unexpectedly found nil while unwrapping an Optional value

on this line:

cell!.nameField.text = eventNameArray[indexPath.section];

Code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    var cell:MainPageTableViewCell?

    if normalTable == self.normalTable {
        cell = normalTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as?MainPageTableViewCell
        cell!.nameField.text = eventNameArray[indexPath.section];
        cell!.messageField.text = eventMessageArray[indexPath.section];
return cell!
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

0

Make sure that you have set delegate and datasource of the tableview. Also to check which tableview it is currently, make it,

if tableview == self.normalTable {
  // type your code here
}

Also if you are trying to populate content on each row from array, the code should be,

if eventNameArray.count > 0 {
    cell!.nameField.text = eventNameArray[indexPath.row];
}

if eventMessageArray.count > 0 {
    cell!.messageField.text = eventMessageArray[indexPath.row];
}
Sneha
  • 2,200
  • 6
  • 22
  • 36
0

Please check with below 2 lines. You are using 2 different Array for fetching value.

 cell!.nameField.text = eventNameArray[indexPath.section];
 cell!.messageField.text = eventMessageArray[indexPath.section];
Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22