Click here to Skip to main content
14,927,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

    
    //MARK: Search Bar
    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()
    } //textDidChange
    
    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
        } // switch
        tableView.reloadData()
    }

    // MARK: something idk
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // searchController.searchBar.scopeButtonTitles = ["Questions", "Rating"]
       // searchController.searchBar.delegate = self
        /*
        if let savedDeck = loadDeck() {
            originalData += savedDeck
        } else {
            startingCards()
        }
        */
        startingCards()
        filteredData = originalData
        // startingCards()
        SharedDeck.loadDeck()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @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)
            }
        }
    }
    
    // MARK: - Table view data source

    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")
        }

        // Get the appropriate card from the deck
        let currentCard = SharedDeck.getDeck().getCardAt(index: indexPath.row)
        
        // Set the cell properties
        cell.cellImageView.image = currentCard.getImage()
        cell.cellQuestionLabel.text = currentCard.getQuestion()
        cell.cellRatingLabel.text = currentCard.getRating()

        return cell
    }

    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            SharedDeck.getDeck().deleteCardAt(index: indexPath.row)
            // Delete the row from the data source
            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")
            }
        
        //originalData.append(q1)
        
        guard let q2 = Card(question: "what starts with a?", answer: "apple", image: UIImage(named: "apple"), rating: "3")
            else {
                fatalError("Unable to instantiatie card")
            }
        
        //originalData.append(q2)
        
        guard let q3 = Card(question: "what starts with q?", answer: "quit", image: UIImage(named: "quit"), rating: "2")
            else {
                fatalError("Unable to instantiatie card")
            }
        
        //originalData.append(q3)
        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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900