繁体   English   中英

单击单元格并转到其他视图时应用程序崩溃 controller

[英]app crashes when cell is clicked and segueing to a different view controller

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

搜索视图控制器:

 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)
    }
    
}

Whenever I select the cell in the first view controller, my app crashes and I get Thread 1: signal SIGABRT 'unable to dequeue a cell with identifier SearchTableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard '这个错误表明我必须为我的歌曲视图 controller 注册一个笔尖,但我已经注册了。

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)
        
    }

确保您的单元在 Storyboard 上正确配置:

  1. Go 到 storyboard 上的单元格

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

    在此处输入图像描述

  3. 在 Attibutes Inspector 中,cell Identifier必须设置为SearchTableViewCell

    在此处输入图像描述

问题是没有这样的笔尖。 因此,您致电

let nib = UINib(nibName: "SearchTableViewCell", bundle: nil)

产生nil ,你的电话

tableView.register(nib, forCellReuseIdentifier: "SearchTableViewCell")

什么也没做。 你在说这些,但你实际上并没有注册任何笔尖。 因此崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM