0

My search controller class is as following:

class FeastSearchTableViewController: UITableViewController, UISearchResultsUpdating {

    var feastEntries = [FeastEntry]()

    var filteredFeasts = [FeastEntry]()


   /* let tableData = ["One","Two","Three","Twenty-One"]
    var filteredTableData = [String]()*/
    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()

            self.tableView.tableHeaderView = controller.searchBar

            return controller
        })()

  self.feastEntries = [FeastEntry(category:"Chocolate", name:"chocolate Bar"),
            FeastEntry(category:"Chocolate", name:"chocolate Chip"),
            FeastEntry(category:"Chocolate", name:"dark chocolate"),
            FeastEntry(category:"Hard", name:"lollipop"),
            FeastEntry(category:"Hard", name:"candy cane"),
            FeastEntry(category:"Hard", name:"jaw breaker"),
            FeastEntry(category:"Other", name:"caramel"),
            FeastEntry(category:"Other", name:"sour chew"),
            FeastEntry(category:"Other", name:"gummi bear")]

        // Reload the table
        self.tableView.reloadData()

    }

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


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 2
        if (self.resultSearchController.active) {
            return self.filteredFeasts.count
        }
        else {
            return self.feastEntries.count
        }
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let feasts = self.feastEntries[indexPath.row]

            let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCellPhoto") as! TimelineCell

            cell.typeImageView.image = UIImage(named: "timeline-photo")
            cell.profileImageView.image = UIImage(named: "profile-pic-2")
            cell.nameLabel.text = feasts.name
            cell.photoImageView?.image = UIImage(named: "dish")
            cell.dateLabel.text = "2 mins ago"
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            return cell

    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("feastDetail", sender: tableView)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "feastDetail" {
            let candyDetailViewController = segue.destinationViewController as! UIViewController

        }
    }

    func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        filteredFeasts.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "category CONTAINS[c] %@", searchController.searchBar.text)
        //let array = (feastEntries as NSMutableArray).filteredArrayUsingPredicate(searchPredicate)
    /*    let array = (feastEntries as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredFeasts = array as! [String]
        */
        self.tableView.reloadData()
    }

}

struct FeastEntry {
    let category : String
    let name : String
}

I have two requirements. First one is as how can I filter feastEntries array on the basis of category inside updateSearchResultsForSearchController. Second is as how can I implement scope bar filter for this scenario.

1 Answer 1

1

Check filter method in swift...using this you can filter any collection type,and you could write something like this:

 let filteredEntry = self.feastEntries.filter { $0.0 == “Chocolate”//filter condition  }

ScopeBar Implementation:

Create a segmented control with chocolate,hard and other segment ... Create an IBaction for that segmented control depending on selected segment apply filter on feastEntries array as shown above.

Sign up to request clarification or add additional context in comments.

4 Comments

abhishek,does it makes sense??
Filter seems to work fine for me, however the tutorial you provided for scopebar uses interface builder to add scope buttons. However, here I have to handle it programitacally. Right?
A normal scope bar which will be used to categorize the food items, say 3 buttons on scope bars as "Chocolate", "Hard" and "Others".
filtering on the basis of Category is some what like scope bar, you can use filter even for scopebar ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.