簡體   English   中英

tableView cell.detailTextLabel.text返回nil

[英]tableView cell.detailTextLabel.text returns nil

我嘗試在tableView detailTextLabel字符串設置為我的detailTextLabel ,但是它返回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中做同樣的事情,並且進展順利...有人會想到嗎?

當沒有創建detailTextLabel時,會發生這種情況。 通常是您的代碼或情節提要中的錯誤。 因此,請檢查有問題的單元的創建。

另請閱讀有關此主題的Stackoverflow問答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM