0

I try to set a string to my detailTextLabel in a tableView but it's returning nil. I have read other posts where I am not the first one but I cannot understand what is going wrong in my case. I am using Swift 4.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell = {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") else {
                return UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "Cell")
            }
            return cell
        }()

        let filtersRow: Bool = (currentSearchType == .all && indexPath.section == 0)

        var titleText: String = ""

        if filtersRow == true {
            titleText = "Filters"
            var detailText: String = ""
            if currentFilters.count == 0 {
                detailText = "None"
            }
            else if currentFilters.count == 1 {
                detailText = currentFilters.first!
            }
            else {
                detailText = "\(currentFilters.count)"
            }
            cell.textLabel?.text = titleText /// -> shows 'Filters' as expected
            cell.detailTextLabel?.text = detailText /// -> shows nothing
            print("Detail text: \(cell.detailTextLabel?.text)") --> returns nil
            print("cell.textLabel? \(String(describing: cell.textLabel))") /// --> Optional(<UITAbleViewLabel...>)
            print("cell.detailTextLabel? \(String(describing: cell.detailTextLabel))") /// ---> nil
        cell.accessoryType = .disclosureIndicator

            cell.accessoryType = .disclosureIndicator
            return cell
        }
        ...

There is definitely something wrong with the way I get my cell, but I do the same thing in an other viewController and it is going well... Does anyone would have an idea?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Trichophyton
  • 625
  • 5
  • 21
  • 1
    Could you add a breakpoint to check if the guard statement fails ? If so could you check if the cell was registered with the tableView either programatically or through storyboard ? – user1046037 Feb 08 '18 at 15:53
  • 1
    can you show the implementation of the cell that is registered under the `"Cell"` identifier? – Milan Nosáľ Feb 08 '18 at 15:56
  • Do not use that strange closure syntax to create a cell. Use the recommended syntax `let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)` without `guard` which returns always a valid non-optional cell and set the style in Interface Builder. – vadian Feb 08 '18 at 15:57
  • Thanks all, I will have a try with the standard way and removing the guard statement. But the problem is that I don't know how to set a 'value1' style to have a right detailTextLabel. Do you know how I can do it? That's why I was using this guard statement (i.e to be able to initialize a tableViewCell since I only register UITableViewCell.self for reuseIdentifier in viewDidLoad (without being able to specify the style) – Trichophyton Feb 08 '18 at 17:36

1 Answers1

0

This happens when the detailTextLabel isnt created. Mostly a bug in your code or storyboard. So check the creation of the problematic Cell.

Read also this Stackoverflow Q&A about this topic

Karsten
  • 1,869
  • 22
  • 38