0

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.

Gimme the 411
  • 994
  • 9
  • 25

3 Answers3

2

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

cocavo
  • 163
  • 8
1

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
LLIAJLbHOu
  • 1,313
  • 12
  • 17
0

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

Hendra Kusumah
  • 186
  • 3
  • 6