【问题标题】:Why TableView return only one cell?为什么 TableView 只返回一个单元格?
【发布时间】:2016-12-01 21:13:20
【问题描述】:

我正在解析来自 iTunes Store 的 JSON 以检索有关音乐家的信息。在解析我收到这样的字典时,它正在打印到我的控制台。

"resultCount": 50

这是我返回对象的方法。但是字典包含50多个元素,程序只返回字典的一个元素。

extension SearchViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if !hasSearched {
            return 0
        }
        else if searchResults.count == 0 {
            return 1
        } else {
            return searchResults.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if searchResults.count == 0 {
            return tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifires.nothingFoundCell, for: indexPath)

        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifires.searchResultCell, for: indexPath) as! SearchResultCell

            let searchResult = searchResults[indexPath.row]
            cell.nameLabel.text = searchResult.name

            if searchResult.artistName.isEmpty {
                cell.artistNameLabel.text = "Unknown"
            } else {
                cell.artistNameLabel.text = String(format: "%@ (%@)", searchResult.artistName, kindForDisplay(kind: searchResult.kind))
            }

            return cell
        }
    }

    func kindForDisplay(kind: String) -> String {
        switch kind {
        case "album": return "Album"
        case "audiobook": return "Audio Book"
        case "book": return "Book"
        case "ebook": return "E-Book"
        case "feature-movie": return "Movie"
        case "music-video": return "Music Video"
        case "podcast": return "Podcast"
        case "software": return "App"
        case "song": return "Song"
        case "tv-episode": return "TV Episode"
        default: return kind
        }
    }



extension SearchViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if searchResults.count == 0 {
        return nil
    } else {
        return indexPath
    }
}

}

这个方法是不是我写错了,还是应该仔细看看其他的?

【问题讨论】:

  • 你是如何初始化searchResults的?从您的代码的外观来看,它可能会找到 searchResults.count = 0 并在 numberOfRows 方法中返回 1。

标签: ios json uitableview dictionary


【解决方案1】:

您的搜索结果是一个字典,但是当您对其调用 count 方法时,它不会返回您希望看到的 "resultCount" 的值。

相反,在行数函数中使用以下命令:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if !hasSearched {
        return 0
    }

    guard let count = searchResults["resultCount"], count > 0 else {
        return 1
    }

    return  count
}

新的返回调用将给出您的计数结果,如果没有任何内容,则返回默认值 1。

这是用值 0、50 和 nil 测试的,分别返回 1、50 和 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2015-03-19
    相关资源
    最近更新 更多