Я разбираю JSON из iTunes Store, чтобы получить информацию о музыкантах. Во время разбора я получаю такой словарь, который печатает на моей консоли.Почему TableView возвращает только одну ячейку?
"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' и вернуть 1 в метод' numberOfRows'. – Frankie