[Solved] Displaying array of a class in tableView cell


I went through all the below answers. But none of them produced the solution. The problem in all solution is the array is printed on same cell, leaving the other cell empty(including the answer provided by Vadian – it will give error because it is printing all the value in same row.). While printing an array on a cell, you have to go in a loop, But none of the answers provides that. This will give error back Index out of range .The best solution I came across is to use switch or enum. Due to switch or enum you can put a condition for every row and according to that you can print the item from the array. Here I put the simple array item “title” as case and according to that printed the array of class.

Solution:- The following code helped me to achieve what I asked.

Note:- enum is more preferred than switch. I used switch because easy to understand and got my work done.

let a = arr2[indexPath.row]
let item = arr[0]

switch a
{
case "title1":
   cell.detailTextLabel?.text = item.a
   return cell
case "title2" :
   cell.detailTextLabel?.text = item.b
   return cell
default:
   break
}

solved Displaying array of a class in tableView cell