im trying to make an expandable table view inside of viewCell
import UIKit
class TableSupportVC: UITableViewController {
let interface: TableSupportView = {
let interface = TableSupportView(frame: CGRect.zero)
interface.translatesAutoresizingMaskIntoConstraints = false
return interface
}()
struct cellData {
var opened = Bool()
var title = String()
var sectionData = [String]()
}
struct dataInside {
var opened = Bool()
var title = String()
var info = [String]()
}
var tableViewData = [cellData]()
var tableViewDataInside = [dataInside]()
override func viewDidLoad() {
super.viewDidLoad()
//register tables
tableView.register(TableSupportCell.self, forCellReuseIdentifier: menuCellIdita)
tableView.register(TableSupportSubtitleCell.self, forCellReuseIdentifier: menuCellda)
// Heres my data for cells
tableViewData = [cellData(opened: false, title: "1", sectionData: ["1.1", "1.2","1.3","1.4"]),
cellData(opened: false, title: "2", sectionData: ["2.1", "2.2", "2.3"])
]
tableViewDataInside = [dataInside(opened: false, title: "1.1.1", info: ["1.1.1"])]
setThemeNavigationBar()
// Size Scrollview
interface.scrollView.contentSize = CGSize(width: wScreen, height: hScreen * 1.40)
initComponents()
}
func setThemeNavigationBar() {
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = blueBlumon
navigationBarAppearace.barTintColor = blueBlumon
navigationController?.navigationBar.barTintColor = blueBlumon
navigationController?.navigationBar.barStyle = .blackOpaque
navigationController?.navigationBar.isTranslucent = false
}
private func initComponents() {
setNavItems()
setSubviews()
setAutolayout()
}
private func setSubviews() {
view.backgroundColor = UIColor.white
view.addSubview(interface)
}
private func setNavItems() {
let backIcon = UIImage(named: "whiteArrow")
let backItem = UIBarButtonItem(image: backIcon?.withRenderingMode(.alwaysTemplate), style: UIBarButtonItem.Style.plain, target: self, action: #selector(backPressed))
backItem.tintColor = UIColor.white
navigationItem.leftBarButtonItem = backItem
let logoIcon = UIImage(named: "itemLogoBlue")
let logoItem = UIBarButtonItem(image: logoIcon?.withRenderingMode(.alwaysOriginal), style: UIBarButtonItem.Style.plain, target: nil, action: nil)
navigationItem.rightBarButtonItem = logoItem
}
// Método para definir el autolayout de los componentes de la vista principal del controlador
private func setAutolayout() {
NSLayoutConstraint.activate([
interface.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
interface.trailingAnchor.constraint(equalTo: view.trailingAnchor),
interface.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
interface.leadingAnchor.constraint(equalTo: view.leadingAnchor)
])
}
// Método de selector backPressed
@objc private func backPressed() {
self.navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return tableViewData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableViewData[section].opened == true{
return tableViewData[section].sectionData.count + 1
}
**//INDEX OUT OF RANGE HERE**
if tableViewDataInside[section].opened == true{
return tableViewDataInside[section].info.count + 1
}
else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: menuCellIdita, for: indexPath) as! TableSupportCell
cell.optionTitle.text = tableViewData[indexPath.section].title
return cell
}
if indexPath.row == 2 && tableViewData[indexPath.section].title == "1.2"{
let cell = tableView.dequeueReusableCell(withIdentifier: menuCellda, for: indexPath) as! TableSupportSubtitleCell
cell.title.text = tableViewDataInside[indexPath.section].title
//cell.subtitle.text = tableViewDataInside[indexPath.section].info
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: menuCellIdita, for: indexPath) as! TableSupportCell
cell.optionTitle.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
return cell
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("el indexpath es: \(indexPath.row)")
if indexPath.row == 0 {
if tableViewData[indexPath.section].opened == true{
tableViewData[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
else{
tableViewData[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
if indexPath.row == 2 && tableViewData[indexPath.section].title == "1.2"{
if tableViewDataInside[indexPath.section].opened == true{
tableViewDataInside[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
else{
tableViewDataInside[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
}
as you can see every time the user selects certain cells it activate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if user press row 0 it will activate the code
same case for
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
but when i try to set
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
and run it crash index out of range
the main problem: i cant show my other cell *tableViewDataInside*
when the user clicks for example 1.2