I'm trying to make a tableView of cards in a deck that have two scopes Questions and Rating.
this is my code:
<pre lang="Swift">
import UIKit
class DeckTableViewController: UITableViewController, UISearchBarDelegate {
static let displayCardSegueID = "displayCard"
@IBOutlet weak var searchBar: UISearchBar!
var originalData = [Card]()
var filteredData = [Card]()
let searchController = UISearchController(searchResultsController: nil)
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredData = originalData.filter({ Card -> Bool in
switch searchBar.selectedScopeButtonIndex {
case 0:
if searchText.isEmpty { return Card.question == Card.question }
return Card.getQuestion().lowercased().contains(searchText.lowercased()) &&
Card.getQuestion() == Card.question
case 1:
if searchText.isEmpty { return Card.getRating() == Card.rating }
return Card.getRating().lowercased().contains(searchText.lowercased()) &&
Card.getRating() == Card.rating
default:
return false
}
})
tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
switch selectedScope {
case 0: filteredData = originalData.filter( {Card -> Bool in
Card.getQuestion() == Card.question })
case 1: filteredData = originalData.filter( {Card -> Bool in
Card.getRating() == Card.rating })
default: break
}
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
startingCards()
filteredData = originalData
SharedDeck.loadDeck()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func unwindToDeckList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? AddCardViewController
{
if let newCard = sourceViewController.getCard() {
let deck = SharedDeck.getDeck()
let newIndexPath = IndexPath(row: deck.getSize(), section: 0)
deck.appendCard(card: newCard)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return SharedDeck.getDeck().getSize()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CardTableViewCell", for: indexPath) as? CardTableViewCell else {
fatalError("The aquired cell is not a CardTableViewCell")
}
let currentCard = SharedDeck.getDeck().getCardAt(index: indexPath.row)
cell.cellImageView.image = currentCard.getImage()
cell.cellQuestionLabel.text = currentCard.getQuestion()
cell.cellRatingLabel.text = currentCard.getRating()
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
SharedDeck.getDeck().deleteCardAt(index: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
func startingCards() {
guard let q1 = Card(question: "what starts with r?", answer: "red", image: UIImage(named: "red"), rating: "1")
else {
fatalError("Unable to instantiatie card")
}
guard let q2 = Card(question: "what starts with a?", answer: "apple", image: UIImage(named: "apple"), rating: "3")
else {
fatalError("Unable to instantiatie card")
}
guard let q3 = Card(question: "what starts with q?", answer: "quit", image: UIImage(named: "quit"), rating: "2")
else {
fatalError("Unable to instantiatie card")
}
originalData += [q1, q2, q3]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == DeckTableViewController.displayCardSegueID {
CardViewController.setCard(card: SharedDeck.getDeck().getCardAt(index: (tableView.indexPathForSelectedRow?.row)!))
}
}
}
What I have tried:
when i run it, i see the search bar and the two scopes, however when i type in the search bar nothing happens, i guess its not even searching anything, and when i select a scope it doesn't do anything either.
Also none of my startingCards show up either not sure why.