Gimme the 411
Gimme the 411

Reputation: 1044

Outlet is connected, but Unexpectedly found nil while unwrapping an Optional value

I'm trying to set up a UITableView inside a UITableViewCell, and I followed the selected answer to Is it possible to add UITableView within a UITableViewCell

This is my code for the TableViewCell class which holds the inner TableView:

import UIKit

class tvc_item: UITableViewCell, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var ivItem: UIImageView!
    @IBOutlet weak var lblItemName: UILabel!
    @IBOutlet weak var tvInner: UITableView!

    var itemdata = Item()

    override func awakeFromNib() {
        super.awakeFromNib()
        setUpTable()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpTable()
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        //tvInner?.frame = CGRectMake(0.2, 0.3, self.bounds.size.width-5, self.bounds.size.height-5)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

    func setUpTable(){
        tvInner.delegate = self // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
        tvInner.dataSource = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_itemrow") as! cell_itemrow
        cell.lblRowLabel.text = String(itemdata.rows[indexPath.row].size)
        return cell
    }
}

When I run the code it dies on this line:

tvInner.delegate = self

with error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

So I assume tvInner is nil, but why? And what can I do about it?

tvInner's outlet seems to be connected correctly and shows a dark circle inside the outlet icon.

Upvotes: 0

Views: 593

Answers (3)

LLIAJLbHOu
LLIAJLbHOu

Reputation: 1313

Is seem like you use xib or storyboard to configure tvc_item layout.

So, the best way is

  • remove delegate/dataSource configuration from code and configure it in Interface Builder just drag & drop

remove:

override func awakeFromNib() {
    super.awakeFromNib()
    setUpTable()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setUpTable()
}

func setUpTable(){
    tvInner.delegate = self // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
    tvInner.dataSource = self
}

configure in IB:

UITableView in UICollectionViewCell

If you still want to configure it in code

  • remove all init functions
  • only awakeFromNib must be there

Upvotes: 1

Hendra Kusumah
Hendra Kusumah

Reputation: 186

It seems you forgot to put setupTable() method on override init.

Upvotes: 0

cocavo
cocavo

Reputation: 163

You need to override awakeFromNib and move setUpTable() inside this method.

Upvotes: 2

Related Questions