首页 > 解决方案 > 单击单元格并转到其他视图控制器时应用程序崩溃

问题描述

我有一个带有表格视图和搜索栏的视图控制器。我想根据单元格中的文本将用户发送到其他三个视图控制器之一。

搜索视图控制器:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
    if (searchBar.text == search[indexPath.row].cleanName) {
        performSegue(withIdentifier: "songs", sender: self)
    } else if (searchBar.text != search[indexPath.row].cleanName) {
        performSegue(withIdentifier: "artist", sender: self)
    } else {
        performSegue(withIdentifier: "albums", sender: self)
    }
    
}

每当我在第一个视图控制器中选择单元格时,我的应用程序崩溃并且我得到Thread 1: signal SIGABRT 'unable to dequeue a cell with identifier SearchTableViewCell - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元格'这个错误表明我必须为我的歌曲视图控制器注册一个 nib,但我已经这样做了。

SearhesViewController:

  class SearchesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate  {


@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var searchesBar: UISearchBar!

var searchActive: Bool = false
var search = [Search]()
let filePath = "http://127.0.0.1/musicfiles"
var songs = [Songs]()
var artists = [Artist]()


override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: "SearchTableViewCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "SearchTableViewCell")
    tableView.delegate = self
    tableView.dataSource = self
    searchesBar.delegate = self
    retriveData()
    print(search)
}
     func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    songs = songs.filter({ (songName) -> Bool in
        return songName.songname.lowercased().range(of: searchText.lowercased()) != nil
        })

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 120
}

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if (searchActive) {
            return search.count
        } else {
            return 1
        }
      
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SearchTableViewCell", for:  indexPath) as! SearchTableViewCell
        
        cell.mainLabel!.text = search[indexPath.row].cleanName
        cell.secondLabel!.text = songs[indexPath.row].artistId
        cell.cellImage!.image = UIImage(named: songs[indexPath.row].cover)
        return cell;
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let searchSong = search[indexPath.row].cleanName
        let searchArtist = search[indexPath.row].artistid
        let fileURLString = "\(filePath)\(searchSong)\(searchArtist)"
        print(fileURLString)
        
    }

标签: iosswift

解决方案


确保您的单元格在情节提要上正确配置:

  1. 转到故事板上的单元格

  2. 在 Identity Inspector 中,单元格必须设置为自定义类 SearchTableViewCell

    在此处输入图像描述

  3. 在 Attibutes Inspector 中,必须将单元标识符设置为SearchTableViewCell

    在此处输入图像描述


推荐阅读