0

I have a custom TableViewCell. Inside of this, I want to put another TableView, but I'm having problems. I have something like this:

import UIKit

class ByteCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var game: UIImageView!
    @IBOutlet weak var TableView: UIView!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

}

First of all, I can't write:

tableView.delegate = self
tableView.dataSource = self

anywhere in here, so I can't modify the tableView at all.

Secondly, the TableView that does appear, doesn't have any rows, and can't be scrolled. I essentially want to make a scrollable TableView inside of a custom cell which is inside of a TableView. Is this possible?

Thanks!

Evan
  • 1,892
  • 2
  • 19
  • 40
  • 1
    You can't write `tableView.delegate = self; tableView.dataSource = self` because your tableView in this class is set as a normal `UIView`. But yes you can add a UITableView within a UITableViewCell. – Zonily Jame Jan 14 '20 at 03:37
  • yess this was my problem, thank you! – Evan Jan 14 '20 at 22:31

1 Answers1

1

You can absolutely put a UITableView inside UITableViewCells

Where to put .delegate = self depends how you created the cell.

If you created it programmatically then use override init

 override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
    }

If however your load the cell from nib or storyboard then use initWithCoder

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
       tableView = UITableView()
       tableView.delegate = self
       tableView.dataSource = self
    }
    return self;
}

Just a note, you are setting yourself up for a bumpy road. Its entirely possible nonetheless so good luck!

Eloy B.
  • 565
  • 4
  • 13
  • 1
    Update: I didn't end up using your code, I realized I just had a problem with my storyboard, but I was able to pull off a tableview with custom cells and each custom cell has a tableview with another custom cell inside of it haha – Evan Jan 14 '20 at 22:31
  • 1
    Awesome, yes enjoy that rabbit hole you are going down. ;) – Eloy B. Jan 14 '20 at 23:06
  • 1
    Haha thanks, if you would like to help me in going down this rabbit hole, I realize that I still have a problem: https://stackoverflow.com/questions/59743623/creating-tableview-inside-tableview – Evan Jan 15 '20 at 00:24
  • I completely understand if you would like to avoid the rabbit hole though, haha – Evan Jan 15 '20 at 00:25