У меня есть таблица в моей базе данных sql, которая показывает исполнителей, песни и альбомы. У каждого из них есть идентификатор. У меня также есть 3 пользовательских ячейки. введите описание изображения здесь
Если идентификатор больше 0, я бы хотел, чтобы эта песня, исполнитель или альбомы отображались в виде таблицы. Я получаю эти данные с помощью массивов.
Каждый раз, когда запускается этот код, я получаю Поток 1: Неустранимая ошибка: Индекс выходит за пределы диапазона cra sh. Это может иметь какое-то отношение к logi c моего оператора if. Мы будем благодарны за любые предложения.
var searchActive: Bool = false
var search = [Search]()
var songs = [Songs]()
var artists = [Artist]()
var album = [Album]()
var cleanSong = ""
var artistName = ""
var albumName = ""
var songCover = UIImage()
var artistPic = UIImage()
var albumCover = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(SongTableViewCell.nib(), forCellReuseIdentifier: SongTableViewCell.songCell)
tableView.register(ArtistTableViewCell.nib(), forCellReuseIdentifier: ArtistTableViewCell.artistCell)
tableView.register(AlbumTableViewCell.nib(), forCellReuseIdentifier: AlbumTableViewCell.AlbumCell)
tableView.delegate = self
tableView.dataSource = self
searchesBar.delegate = self
print(search)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (search[indexPath.row].songid > 0) { //CRASH: Thread 1: Fatal error: Index out of range
let cell = tableView.dequeueReusableCell(withIdentifier: "SongTableViewCell", for: indexPath) as! SongTableViewCell
cell.mainLabel!.text = songs[indexPath.row].cleanName
cell.secondLabel!.text = songs[indexPath.row].artistName
cell.cellImage!.image = UIImage(named: songs[indexPath.row].cover)
return cell
} else if (search[indexPath.row].artistid > 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "ArtistTableViewCell", for: indexPath) as! ArtistTableViewCell
cell.artistLabel.text = artists[indexPath.row].artistName
cell.artiistImage.image = UIImage(named: artists[indexPath.row].picture)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "AlbumTableViewCell", for: indexPath) as! AlbumTableViewCell
cell.albumLabel!.text = search[indexPath.row].cleanSong
cell.albumCover.image = UIImage(named: album[indexPath.row].cover)
return cell
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (searchActive) {
return search.count
} else {
return 1
}
}