0

我尝试detailTextLabel在 tableView 中为 my 设置一个字符串,但它正在返回nil。我读过其他帖子,我不是第一个,但我不明白我的情况出了什么问题。我正在使用 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
        }
        ...

我获取单元格的方式肯定有问题,但我在另一个 viewController 中做了同样的事情,而且进展顺利......有人会有想法吗?

4

1 回答 1

0

当没有创建 detailTextLabel 时会发生这种情况。主要是您的代码或情节提要中的错误。所以检查有问题的单元格的创建。

另请阅读有关此主题的 Stackoverflow Q&A

于 2019-10-11T07:58:57.857 回答