Watson Watson - 11 months ago 63
Swift Question

Not being able to connect IBOutlet

So here's a problem I have been stuck at for 2 hours now!
I have a Table View Controller with static cell table View.
In one of the static table View Cells I have placed a tableView with dynamic cells. I have created a custom class for that tableViewCell. The problem is that I cannot connect the dynamic cell tableView to my custom class no matter what I do.
Anybody faced this similar issue? Please share some insight on how to resolve this.

The following line used to reuse my custom tableView cell in the tableViewController throws an error Unexpectedly found nil while unwrapping an optional value

let cell = tableView.dequeueReusableCell(withIdentifier: "customCellTVC") as! MyCustomTableViewCell

Answer Source

So I have found the answer to the problem and here is what I did

dequeueReusableCell(withIdentifier:) works for dynamic tableView cells. As my main tableView was static, dequeueReusableCell(withIdentifier:) was returning nil everytime it ran. So instead , I did this

let cell = super.tableView(tableView, cellForRowAt: indexPath) as? MyCustomTableViewCell

I had already made the UI for my custom cell in the tableView storyboard. Use of identifier for tableViewCells is trivial. The tableView already knows the position of the cells.