I am new to swift and have been experimenting with passing data between view controllers. I have been attempting to pass Json data from a view controller into a UITableViewCell, However once run my code has no effect.
DetailViewController (passes data to the libraryViewController)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestViewController: LibraryMovieViewController = segue.destination as! LibraryMovieViewController
DestViewController.movieTitle = movieTitle
DestViewController.movieRelease = movieReleaseDate
}
UITableViewCell
class MovieSearchTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var posterView: UIImageView!
@IBOutlet weak var overviewLabel: UILabel!
}
LibraryViewController
struct libMovie {
//let mainImage: UIImage
let title: String
let release: String
}
class LibraryMovieViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var dataSource: [libMovie] = []
var movieTitle: String!
var movieRelease: String!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view.
loadDataSource()
}
func loadDataSource(){
dataSource.append(libMovie(title: " \(String(describing: movieTitle))", release: " \(String(describing: movieRelease))"))
tableView.reloadData()
}
/*
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let DestViewController: LibraryDetailViewController = segue.destination as! LibraryDetailViewController
DestViewController.detailTitle = movieTitle
DestViewController.detailRelease = movieRelease
}
*/
}
extension LibraryMovieViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 115
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let movieCell = tableView.dequeueReusableCell(withIdentifier: "libCell", for: indexPath) as? LibraryMovieTableViewCell else {
return UITableViewCell()
}
let libMovie = dataSource[indexPath.row]
movieCell.cellTitleLabel.text = "Movie Title:\(libMovie.title)"
movieCell.cellReleaseLabel.text = "Release Date:\(libMovie.release)"
return movieCell
}
}
I would expect that when the app is run that movieTitle and movieReleaseDate are passed from the detail view controller and input into the library table cell, this is initiated by tapping a button on the detail view controller.
However this seems to have no affect on the program or simply returns blank cells. No errors are reported in console nor does the app crash
I have tried sending the data to the variables movieTitle and movieRelease and setting NumberOfRowsInSection to 1 but this returns nil
I receive an error: Value of type View Controller has no member libMovieDestViewController.libMovie.title = movieTitle
DestViewcontroller.datasource also produces and error -> Cannot assign value of type 'String?' to type '[libMovie]'