Posts

Post not yet marked as solved
0 Replies
851 Views
I have an app that lets users select movies from a list in tableview (populated by api), tapping on a movie presents a view with more details. I would like to implement a search bar so users can search for specific movies, however I am having some troubleI assumed the following function would make movies searchable for the following tableviewvar newMovies: [NSDictionary] = []...func searchBar(){ let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)) searchBar.delegate = self searchBar.showsScopeBar = true searchBar.tintColor = UIColor.lightGray searchBar.scopeButtonTitles = ["Movie", "Rating"] self.tableView.tableHeaderView = searchBar } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { if searchText == "" { fetchMovies() } else { if searchBar.selectedScopeButtonIndex == 0 { newMovies = newMovies.filter({ (NSDictionary) -> Bool in return NSDictionary.lowercased().contains(searchText.lowercased()) }) } else { } } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return newMovies.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! MovieSearchTableViewCell let movie = newMovies[indexPath.row] let title = movie["title"] as! String let overview = movie["overview"] as! String cell.titleLabel.text = title cell.overviewLabel.text = overviewHowever this presents 2 errors on the line:return NSDictionary.lowercased().contains(searchText.lowercased())Value of type 'Any' has no member 'contains'Value of type 'NSDictionary' has no member 'lowercased' should I be returning something other than NSDictionary?
Posted
by Goligy08.
Last updated
.
Post marked as solved
8 Replies
2.5k Views
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 }UITableViewCellclass MovieSearchTableViewCell: UITableViewCell { @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var posterView: UIImageView! @IBOutlet weak var overviewLabel: UILabel! }LibraryViewControllerstruct 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 crashI have tried sending the data to the variables movieTitle and movieRelease and setting NumberOfRowsInSection to 1 but this returns nilI receive an error: Value of type View Controller has no member libMovieDestViewController.libMovie.title = movieTitleDestViewcontroller.datasource also produces and error -> Cannot assign value of type 'String?' to type '[libMovie]'
Posted
by Goligy08.
Last updated
.
Post not yet marked as solved
6 Replies
2.8k Views
So i'm trying to pass data through multiple views but having a lot of bother. Essentially i'm trying to create a library/playlist in a table viewI have a table view where the user searches for movies (movie details from API ) and taps their selected movie which then segue to another view controller which displays more info on the users selection.I'm then attempting to implement an 'add to library' button to then input this data into the table cell in a third view controller. The first segue (to movieDetailsVC) works perfectly however i'm having trouble sending data to the third view controllerI have assumed that in order to display this data in a table view it must first segue to the TableViewCell and then segue again to the final view controller?MovieSearch VC (initla view controller sends movie data to detail VC) ...overridefunc prepare(for segue: UIStoryboardSegue, sender: Any?) { let indexPath = tableView.indexPathForSelectedRow let index = indexPath?.row let movieDetails = newMovies[index!] let movieDetailViewController = segue.destination as! MovieDetailViewController movieDetailViewController.posterUrl = movieDetails.value(forKeyPath: "backdrop_path") as? String movieDetailViewController.coverUrl = movieDetails.value(forKeyPath: "poster_path") as? String movieDetailViewController.movieTitle = movieDetails.value(forKeyPath: "title") as? String movieDetailViewController.movieRating = movieDetails.value(forKeyPath: "vote_average") as! NSNumber as NSNumber movieDetailViewController.movieOverview = movieDetails.value(forKeyPath: "overview") as? String }MovieDetailVC (sends data to tableViewCell)var newMovies: [NSDictionary] = [] @IBOutlet var detailView: UIView! @IBOutlet weak var posterView: UIImageView! @IBOutlet weak var coverView: UIImageView! @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var releaseDateLabel: UILabel! @IBOutlet weak var overviewLabel: UILabel! @IBOutlet weak var ratingLabel: UILabel! var posterUrl: String! var coverUrl: String! var movieTitle: String! var movieReleaseDate: String! var movieOverview: String! var movieRating: NSNumber!...attempts to send var movieTitle defined above to libraryTableViewCell (libMovieTitle) // destination should be LibraryViewController ?? override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let LibraryTableViewCell = segue.destination as! LibraryMovieTableViewCell LibraryTableViewCell.libMovieTitle = movieTitle } TableViewCell(attempts to send data to final ViewController) class LibraryMovieTableViewCell: UITableViewCell { @IBOutlet weak var cellTitleLabel: UILabel! var libMovieTitle: String? override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } func prepare(for segue: UIStoryboardSegue, sender: Any?) { let LibraryMovieViewcontroller = segue.destination as! LibraryMovieViewController LibraryMovieViewController.libraryTitle = cellTitleLabel } } LibraryViewController (attempts to display sent data in table)class LibraryMovieViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var libraryMovies: [NSDictionary] = [] let libraryTitle: String? override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self tableView.delegate = self // Do any additional setup after loading the view. } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return libraryMovies.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let libraryCell = tableView.dequeueReusableCell(withIdentifier: "libCell", for: indexPath) as! LibraryMovieTableViewCell libraryCell.textLabel!.text = libraryTitle return libraryCell } }At Present I receive an error: Instance member 'libraryTitle' cannot be used on type 'LibraryMovieViewController' at:LibraryMovieViewController.libraryTitle = cellTitleLabel
Posted
by Goligy08.
Last updated
.