DmitriyK
DmitriyK

Reputation: 63

Why is table view displayed but cells is not?

I'm builded view like this:

enter image description here

and constrains:

enter image description here

Here is a code:

Main controller

class SelectTagsViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var selectedTagsTableView: UITableView! //added table view

    let allTags: [String] = ["Qwerty", "IT", "Manic", "VR", "WishHook", "Books", "Programing", "Aplication", "Something", "Fly", "Swift", "Earth", "Orbit", "Mars", "Summer", "Monkey", "AR", "Space"]
    var selectedTags: [String] = ["Qwerty", "IT", "Manic", "VR"]

    override func viewDidLoad() {
        super.viewDidLoad()
        selectedTagsTableView.tableFooterView = UIView(frame: CGRect.zero)
        selectedTagsTableView.backgroundColor = .lightGray
        selectedTagsTableView.register(SelectedTagCell.self, forCellReuseIdentifier: "SelectedTagCell")
    }
}



Extension for tableview

extension SelectTagsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let lable = selectedTags[indexPath.row]
        let cell = selectedTagsTableView.dequeueReusableCell(withIdentifier: "SelectedTagCell") as! SelectedTagCell
        cell.tagNameLabel.text = lable
        return cell
    }

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



And class for cells

class SelectedTagCell: UITableViewCell {
    @IBOutlet weak var tagNameLabel: UILabel!
    @IBOutlet weak var removeTagButton: UIButton!
    @IBAction func removeTagButtonTapped(_ sender: Any) {
    }
}

But cells aren't displayed:

enter image description here

All code are in one file, and all classes and identifiers are connected to components. What did I miss?

Upvotes: 0

Views: 79

Answers (1)

DmitriyK
DmitriyK

Reputation: 63

On the recommendation of @Adrian, I added delegate and datasource below and all now work.

selectedTagsTableView.delegate = self
selectedTagsTableView.dataSource = self

And fixed problem with nil value, removed code below:

selectedTagsTableView.register(SelectedTagCell.self, forCellReuseIdentifier: "SelectedTagCell")

Upvotes: 1

Related Questions