[Swift 4] Custom UITableView Delegate & DataSource

I want to use a UITableview with different custom UITableview but a different class like create protocol and struct, you must try this code below


How to use that class TableViewDelegate

public class HomeViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    
    let dataString = ["data1", "data2", "data3", "data4"]
    let cell = "cellTable1"
    var tableViewDelegate: TableViewDelegate?
    
    override public func viewDidLoad() {
        super.viewDidLoad()
        
        // creating the delegate object and passing the data
        tableViewDelegate = TableViewDelegate(data: dataString, data2: dataLeftString)
        
        
        // passing a function to the delegate object
        tableViewDelegate?.didSelectRow = didSelectRow(_:_:)
        
        // passing a function to the delegate object
        tableView.delegate = tableViewDelegate
        tableView.dataSource = tableViewDelegate
        tableViewDelegate?.cell = cell
        tableViewDelegate?.cell2 = cell2
        tableView.register(UINib(nibName: "IATableViewCell", bundle: nil), forCellReuseIdentifier: self.cell)
        tableView.register(UINib(nibName: "IBTableViewCell", bundle: nil), forCellReuseIdentifier: self.cell2)
        tableView.rowHeight = UITableViewAutomaticDimension
        
    }
    
    // a function that will be called by the delegate object
    // when a row is selected
    
    func didSelectRow(_ dataItem: String, _ cell: UITableViewCell) {
        AlertACT.showBasicAlert(on: self, with: "Message", message: "Example Item: \(dataItem)")
    }

No comments: