Reputation: 600
I am trying to segue from a collectionViewCell
to take me to a tableView
but can't get it to work. It says
Cannot assign value of type string to type [browseBook]
I wanna click on the cell and displays the corresponding information in the tableview cells.
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
}
}
Upvotes: 0
Views: 325
Reputation: 4470
Replace this line
destinationController.browseBooks = sector.title
destinationController.browseBooks = UIImage(named:sector.imageName )
with
destinationController.browseBooks = [BrowseBook(image: sector.imageName , bookName: sector.title, authorName: "Jay McGraven")]
Upvotes: 2