我试图从collectionViewCell
转到带我去tableView
,但无法让它发挥作用。它说
无法将类型字符串的值赋给[browseBook]
类型
我想点击单元格并在tableview单元格中显示相应的信息。
CareerSectorViewController:
class CareerSectorViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var careerSectorViewController: UICollectionView!
var sectors:[CareerSectorModel] = [
CareerSectorModel(title: "Javascript", imageName: "js.png"),
]
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showBooks", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showBooks" {
let destinationController = segue.destination as! BooksViewController
if let cell = sender as? CareerSectorCell {
if let indexPath = careerSectorViewController.indexPath(for: cell) {
let sector = sectors[indexPath.row]
destinationController.browseBooks = sector.title
destinationController.browseBooks = UIImage(named:sector.imageName )
}
}
}
}
}
BookViewController:
class BooksViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
let cellIdentifier = "bookCell"
var browseBooks:[BrowseBook] = [
BrowseBook(image: "ruby.png", bookName: "HeadFirstRuby", authorName: "Jay McGraven"),
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return browseBooks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier , for: indexPath) as! BookCell
let browseBook = browseBooks[indexPath.row]
cell.bookImageName.image = UIImage(named: browseBook.image)
cell.nameLabel.text = browseBook.bookName
cell.authorLabel.text = browseBook.authorName
return cell
}
}
BrowseBookModel:
struct BrowseBook {
private(set) public var image: String
private(set) public var bookName: String
private(set) public var authorName: String
init(image: String, bookName: String, authorName: String) {
self.image = image
self.bookName = bookName
self.authorName = authorName
}
}
答案 0 :(得分:2)
替换此行
destinationController.browseBooks = sector.title
destinationController.browseBooks = UIImage(named:sector.imageName )
与
destinationController.browseBooks = [BrowseBook(image: sector.imageName , bookName: sector.title, authorName: "Jay McGraven")]