Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
20.6k views
in Technique[技术] by (71.8m points)

swift - Appending values each time the scope buttons change

I am writing an app that takes movie or series information from OMBdAPI:

I am appending the movies to my movies array in my updateSearchResults function which is making it so that data gets appended to my array everytime I change the scope button. Where could I put the append function so that i could avoid this problem?

import UIKit

private let reuseIdentifier = "MovieCell"

class MovieCollectionVC: UICollectionViewController {

var movies = [MovieData]()
let movieManager = MovieManager()

// MARK: - Properties
let searchController = UISearchController(searchResultsController: nil)
var filteredMovies : [MovieData] = []
var isSearchBarEmpty: Bool {
  return searchController.searchBar.text?.isEmpty ?? true
}
var isFiltering: Bool {
  return searchController.isActive && !isSearchBarEmpty
}

override func viewDidLoad() {
    super.viewDidLoad()
    configureSearchController()
}

// MARK: UICollectionViewDataSource

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    if isFiltering {
        return filteredMovies.count
    }
    return movies.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MovieCell

    let movie: MovieData
      if isFiltering {
        movie = filteredMovies[indexPath.item]
      } else {
        movie = movies[indexPath.item]
      }
    
    let item = movie
    cell.title.text = item.title
    cell.year.text = item.year
    cell.contentType.text = item.contentType
    cell.poster.image = movieManager.urlToImg(url: item.poster)

    return cell
}

// MARK: - Search Controller

func filterSearchController(_ searchBar: UISearchBar) {

    guard let scopeString = searchBar.scopeButtonTitles?[searchBar.selectedScopeButtonIndex] else {return}
    let selectedContent = MovieData.MediaType(rawValue: scopeString) ?? .all
    
    let searchText = searchBar.text ?? ""
    //print(movies.count)
    filteredMovies = movies.filter { movie in
        let isContentMatching = (selectedContent == .all) || (movie.contentType == selectedContent.rawValue)
        
        let isMatchingSearchText =    movie.title.lowercased().contains(searchText.lowercased()) || searchText.lowercased().count == 0
        
        //print(filteredMovies.count)
        return isContentMatching && isMatchingSearchText
    }
    collectionView.reloadData()
}


func configureSearchController() {
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Movies"
    navigationItem.searchController = searchController
    definesPresentationContext = true
    searchController.searchBar.scopeButtonTitles = ["all", "movie", "series"]
    searchController.searchBar.delegate = self
}

}

extension MovieCollectionVC: UISearchResultsUpdating {
  func updateSearchResults(for searchController: UISearchController) {

//Change the 'space' for a plus sign to fit OMDb's API's search engine
let textWithNoSpaces = searchController.searchBar.text?.replacingOccurrences(of: " ", with: "+")
    movieManager.fetchMovieInfo(title: textWithNoSpaces ?? "", collectionView) { 
 (movieID,moviePoster,movieTitle,releasedYear,contentType)   in

    self.movies.append(MovieData(id: movieID, poster: moviePoster, title: movieTitle, year: releasedYear, contentType: contentType))
}
filterSearchController(searchController.searchBar)

  }
 }

extension MovieCollectionVC: UISearchBarDelegate {
   func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    filterSearchController(searchController.searchBar)
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...