Saturday, February 11, 2023
HomeiOS Developmentios - How do you create a tableView inside a tableViewCell?

ios – How do you create a tableView inside a tableViewCell?


I’ve been looking for a while now and have developed a large headache attempting to unravel this drawback. Mainly, I wish to have a guardian tableView mainTableView to comprise a tableViewCell that features a label and one other tableView innerTableView. This drawback is just not so simple as I had hoped.

If there’s a higher technique to implement what I would like with totally different elements, please let me know.

In any other case, I’ve tried to simplify the code to its naked bones with easy knowledge. Please assume I’ve appropriately linked all my retailers correctly. I’ve 3 swift recordsdata for this simplified model. ViewController.swift is the primary VC that holds the mainTableView. MainTableViewCell.swift is the primary cell that can maintain a label and a tableView innerTableView in it. InnerTableViewCell is the ultimate cell that shall be related to the innerTableView. On this cell, there’s solely a label. I’ve been profitable in creating this primary layer and the mainNameLabel.textual content exhibits up correctly on the primary storyboard. Nonetheless, I’ve configured MainTableViewCell to one of the best of my data and have come quick to success. The innerTableView doesn’t seem on the primary storyboard.

Beneath, I’ll present the code of every swift file:

ViewController.swift

//
//  ViewController.swift
//  NestedTableViews
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    @IBOutlet weak var mainTableView: UITableView!
    
    //pretend knowledge
    var arr = ["Hello", "Hi", "Whats up", "How's it going"]
    

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        // Do any further setup after loading the view.
        mainTableView.delegate = self
        mainTableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
        
        let phrase = arr[indexPath.row]
        
        cell.mainNameLabel.textual content = phrase
        
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return arr.rely
    }
}

MainTableViewCell.swift

//
//  MainTableViewCell.swift
//  NestedTableViews
//

import UIKit

class MainTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {

    
    @IBOutlet weak var mainNameLabel: UILabel!
    @IBOutlet weak var innerTableView: UITableView!
    
    var innerArr = ["How are you?", "How are you doing?", "What's the matter?"]
    
    
    override func awakeFromNib() {
        tremendous.awakeFromNib()
        // Initialization code
        innerTableView.dataSource = self
        innerTableView.delegate = self
    }

    override func setSelected(_ chosen: Bool, animated: Bool) {
        tremendous.setSelected(chosen, animated: animated)

        // Configure the view for the chosen state
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "InnerTableViewCell") as! InnerTableViewCell
        
        let innerWord = innerArr[indexPath.row]
        
        cell.innerNameLabel.textual content = innerWord
        
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return innerArr.rely
    }
}

InnerTableViewCell

//
//  InnerTableViewCell.swift
//  NestedTableViews
//

import UIKit

class InnerTableViewCell: UITableViewCell {

    @IBOutlet weak var innerNameLabel: UILabel!
    
    
    override func awakeFromNib() {
        tremendous.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ chosen: Bool, animated: Bool) {
        tremendous.setSelected(chosen, animated: animated)

        // Configure the view for the chosen state
    }

}

Here’s what the storyboard appears like on this present state:
Current state of the storyboard

What’s it that I’m doing fallacious? I may ask 100 questions however regardless, I’m nonetheless very caught right here. How do I get the nested tableView innerTableView to look and with the right knowledge?

Thanks upfront



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments