Home > Net >  IOS swift5 UIController call uitableview function problem. Please assist
IOS swift5 UIController call uitableview function problem. Please assist

Time:02-12

I have a UIController class and a Tableview class. I would like to include tableview inside uicontroller. But i cannot access tableview inside functions (even static or public). how can i access tableview function from uicontroller?

Million thanks. Struggling with this problem long

import Foundation
import UIKit

class UIController: UIViewController {
private var tableView: UITableView = RegDropdownMenu(identifier: RegStepTwoIndentifier.regStepTwoTable)

init(identifier: String) {
 
    super.init()

    view.addSubview(tableView)
    
    // Cannot call the func
    tableView.testingFunc()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}




class TableView: UITableView {
private var identifier: String

init(identifier: String) {
    self.identifier = identifier
    super.init(frame: .zero, style: .plain)
    configTable()
}

private func configTable() {
    self.delegate = self
    self.dataSource = self
    self.translatesAutoresizingMaskIntoConstraints = false
    self.register(CellClass.self, forCellReuseIdentifier: identifier)
    self.allowsSelection = true
    self.separatorStyle = .none
    self.layer.masksToBounds = true
    self.backgroundColor = .white
    self.layer.borderColor = Styles.borderColor.cgColor
    self.layer.borderWidth = Styles.borderWidth
}

func testingFunc() {
    print("123")
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

CodePudding user response:

You have created a custom UITableView subclass called TableView

This is the line of code that does this class TableView: UITableView

However, in your ViewController you are creating a generic UITableView

private var tableView: UITableView

This should be changed to

private var tableView: TableView which is the name of the custom UITableView class you created.

After this, tableView.testingFunc() should work.

  • Related