1

I am trying to print out my cells detailed text label below the title of the cell. Here is all my code I have no errors when running, the detailed label just doesn't appear. I tried solutions from other forums including this one, nothing worked. I really am not sure why this is happening? maybe it's something in my cellForRowAt function

class LoginController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return discussionTitles.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
        cell.backgroundColor = UIColor.clear
        cell.textLabel?.text = discussionTitles[indexPath.row]
        cell.detailTextLabel?.text = discussionDescriptions[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Discussion Board"
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        self.hideKeyboardWhenTap()
        view.backgroundColor = UIColor(red: 203/255, green: 215/255, blue: 242/255, alpha: 1.0)
        discussionBoardView.register(UITableViewCell.self, forCellReuseIdentifier: "contactCell")
        setupDiscussionBoard()

    }



    func setupDiscussionBoard() {
        view.addSubview(discussionBoardView)
        discussionBoardView.backgroundColor = UIColor(red: 203/255, green: 215/255, blue: 242/255, alpha: 1.0)
        discussionBoardView.dataSource = self
        discussionBoardView.delegate = self
        discussionBoardView.translatesAutoresizingMaskIntoConstraints = false
        discussionBoardView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        discussionBoardView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        discussionBoardView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        discussionBoardView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    }

}

Najeeb ur Rehman
  • 403
  • 4
  • 11
  • 1
    Is the style of the cell set to one of the styles which enables `detailTextLabel`? And you are strongly discouraged from using multiple arrays as datasource. – vadian Mar 19 '20 at 17:16
  • Why would you use two separate Arrays? – Rob Mar 19 '20 at 17:21
  • I will fix the arrays, how do I get the detailed text label to appear? – The Retro Gamer Mar 19 '20 at 17:27
  • Are you sure `discussionDescriptions` is an array of strings and has content? – koen Mar 19 '20 at 17:38
  • yes, I printed them out in that tableview function – The Retro Gamer Mar 19 '20 at 17:44
  • Does this answer your question? [Why is detailTextLabel not visible?](https://stackoverflow.com/questions/5190648/why-is-detailtextlabel-not-visible) – koen Mar 19 '20 at 17:51
  • I tried each solution (other than the storyboard one because I am not using a storyboard) and none have worked. No errors but no detailed text – The Retro Gamer Mar 19 '20 at 18:19
  • Try removing the register line and then set the style in `cellForRowAt ` – koen Mar 19 '20 at 18:24
  • @koen where in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { should I add style: .subtitle. or should I change my cell initialization? – The Retro Gamer Mar 19 '20 at 18:42
  • @TheRetroGamer like this: https://stackoverflow.com/questions/24062285/how-to-set-uitableviewcellstylesubtitle-and-dequeuereusablecell-in-swift. Or use a custom cell. – koen Mar 19 '20 at 18:47

1 Answers1

0

This is because you have not defined the UITableViewCell style and by default it is using default style cell that only consists of titleLabel.

For other UITableViewCell style you have to specify that style explicitly. You can achieve this behaviour by using the following code in cellForRowAt:IndexPath for creating the cell instance.

var cell: UITableViewCell
if let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: "contactCell") as? UITableViewCell {
    cell = dequeuedCell
} else {
    cell = UITableViewCell(style: .subtitle, reuseIdentifier: "contactCell")
}

This will output the cell with descriptionLabel as per your requirement.

Note: Remove the below line too, that you have written in viewDidLoad to register the UITableViewCell as it is also registering the UITableViewCell with default style because it is not required at all.

discussionBoardView.register(UITableViewCell.self, forCellReuseIdentifier: "contactCell")
Najeeb ur Rehman
  • 403
  • 4
  • 11