1

I am new to iOS development and am in my second app with Swift. There are 2 views in my app. First View is an Array with fruits, that is randomly generate on a button click(by the user). Output of First View: [Apple, Orange, Banana, Pears]

Now the user clicks the generate button again. The View gives a random selection of fruits: [Pomegranate, Pears, Watermelon, Muskmelon]

The user clicks the generate button again and new list of array are generated.

All these generated arrays are stored in the Second View which is a Table view controller. The saving of array of fruits in second view controller is done by Core data.

Now, I have added a Search bar and Search display Controller in this second view, so that any fruit; say Pears; when entered by the user is filtered in the table and only those rows are displayed/filtered in the same table view.

I am not sure how do I read/search/filter data from Core data and make the Search Bar Controller work. I have tried many methods and read various articles on Core data and UI Search Bar Controller, but nothing helped. Please advice how do I proceed.

So Far my second view controller:

import UIKit
import CoreData
import Foundation

class HistoryTableViewController: UITableViewController {

     var totalGeneratedFruits: Int = 0
        let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        let managedObjectContext: NSManagedObjectContext? = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
        var arrayFiltered = [Fruits]() // SHOULD THE SEARCH ARRAY BE LIKE THIS??? 

        @IBOutlet weak var searchBar: UISearchBar! // UISearch Bar Controller
        @IBOutlet var tblHistory: UITableView! = nil // The Table view controller

        override func viewDidLoad()
        {
            super.viewDidLoad()
            var request = NSFetchRequest(entityName: "Fruits")
            request.returnsObjectsAsFaults = false
            totalGeneratedFruits = context.countForFetchRequest(request, error: nil )
           //for search
            ????????HOW to Overload search filtered data???????
        }
        // MARK: - Table view data source
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
                return 1
        }
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
                return totalGeneratedFruits
        }
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: (NSIndexPath!)) -> UITableViewCell
        {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
            let request = NSFetchRequest(entityName: "Fruits")
            request.returnsObjectsAsFaults = false
            //Get the contents of Generated fruits and put into cell to build History Table view rows
            var results : NSArray
            do {
                results = try context.executeFetchRequest(request)
                let thisFruit = results[indexPath.row] as! Fruits
                cell.textLabel?.text = thisFruit.fruit 
                cell.detailTextLabel?.text = thisFruit.date // As Subtitle

               //For search criteria
              ????????HOW TO SEARCH and FILTER data???????
                }
            }catch {}
            return cell
        } 
        // Override to support conditional editing of the table view.
        override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            return true
        }
    }

??????????? WHICH DELEGATE FUNCTION SHOULD BE USED?? and how do I implement it?

My Core data class looks like: Core Data class name is Fruits.swift

import UIKit
import CoreData

@objc(Fruits)

class Fruits: NSManagedObject {
    @NSManaged var date: String
    @NSManaged var fruit: String
}

In the first view the array is saved in Core data by the following code:

let newFruit = Fruits(entity: ent!, insertIntoManagedObjectContext: context)
newFruit.fruit = displayLabel.text!
newFruit.date = dateFormatter.stringFromDate(NSDate())
        do {
            try context.save()
        }catch{}

After a couple of more research I added the below method for the search functionality to filter data. But the value in PREDICATE is always nil. What am I doing wrong?

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        if !searchText.isEmpty {
            fetchedResultsController = nil 
            var predicate: NSPredicate = NSPredicate()
            predicate = NSPredicate(format: "fruit contains [cd] %@", searchBar.text!)
            let sortDescriptor = NSSortDescriptor(key: "fruit", ascending: true)
            fetchRequest.sortDescriptors = [sortDescriptor]
            fetchedResultsController?.fetchRequest.predicate = predicate
            print(fetchedResultsController?.fetchRequest.predicate) 
 // HERE THE fetchedResultsController?.fetchRequest.predicate is ALWAYS NIL
            do {
                try fetchedResultsController?.performFetch()
            }catch{}
            fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
            fetchedResultsController?.delegate = self
            tableView.reloadData() 
        }
    }
  • Your question is too broad. Code dumping his not helpful. – Mundi Jan 22 '16 at 00:57
  • There are too many unnecessary information in your example, making it for us really hard to get the grasp of what's going on. Please narrow it down to the problem only so it makes it for the people who are trying to help you easier. – Eendje Jan 22 '16 at 04:50
  • Anyway, the data "generated" from Core Data should be an `Array`. Use the delegate methods of the search bar controller on this array and you should be fine. – Eendje Jan 22 '16 at 04:51
  • I have further reduced my code to show the functions that actually build the table view. Please advice as to which delegate method should be used to implement search functionality and how to implement it? Thanks in advance. – spinach.blue Jan 22 '16 at 19:45

0 Answers0