0
votes

I'm trying to build a tableView with a custom cell using UITableView programmatically without using a Storyboard but after I finish the tutorial and try to run the app the CustomCell I build,

The custom cell shows but once I click on tableView the custom cell hiding

Can someone check if I miss something? Or maybe it's wrong to use xib with programmatically made tableView?

HomeViewController.swift

class HomeViewController: UIViewController {

    fileprivate let homeTableView: UITableView = {
        let htm = UITableView()
        htm.translatesAutoresizingMaskIntoConstraints = false
        return htm
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        homeTableView.register(CusTableViewCell.nib, forCellReuseIdentifier: CusTableViewCell.identifier)

        setupTableView()

    }

    func setupTableView() {

        let viewModel = HomeViewModel()
        homeTableView.delegate = viewModel
        homeTableView.dataSource = viewModel

        homeTableView.rowHeight = 100

        view.addSubview(homeTableView)

        // homeTableView.separatorStyle = .none

        homeTableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        homeTableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
        homeTableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
        homeTableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true

    }

}

CusTableViewCell.swift

class CusTableViewCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!

    static var nib:UINib {
        return UINib(nibName: identifier, bundle: nil)
    }

    static var identifier: String {
        return String(describing: self)
    }

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

        nameLabel.text = "Something"
    }

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

        // Configure the view for the selected state
    }

}

HomeViewModel.swift

extension HomeViewModel: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: CusTableViewCell.identifier, for: indexPath) as? CusTableViewCell {
            cell.backgroundColor = UIColor(red:0.17, green:0.73, blue:0.83, alpha:1.0)
            return cell
        }
        return UITableViewCell()
    }

}
1

1 Answers

1
votes

The problem is related to weak reference to viewModel.

Put viewModel variable to the controller class as follows:

class HomeViewController {

  var viewModel: HomeViewModel!
  ...

  func setupTableView() {
      self.viewModel = HomeViewModel()
      ...
  }
}