0

I could not find any way to add switch button in my tableview

I have a search bar which works perfectly,

and now I want to add a toggle button to enable "AND" "OR" filter in my search.

inline

inline

inline

import Foundation
import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {
    var appDel: AppDelegate!
//    var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "my.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "my.png"))

    var filteredTableData = [String]()
    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        self.edgesForExtendedLayout = UIRectEdge.None
        UIApplication.sharedApplication().statusBarHidden = true
        appDel = UIApplication.sharedApplication().delegate as? AppDelegate
//        tabBarItem = customTabBarItem

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

            self.tableView.tableHeaderView = controller.searchBar

            return controller
        })()

        // Reload the table
        self.tableView.reloadData()
        print("TableView called!!")
    }

    func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        //refer to http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
        filteredTableData.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
        let array = ((appDel.dataFetcher?.appTitles)! as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredTableData = array as! [String]
        self.tableView.reloadData()
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.resultSearchController.active) {
            return self.filteredTableData.count
        }
        else if appDel.dataFetcher?.appTitles == nil {
            return 0
        }else{
            return (appDel.dataFetcher?.appTitles?.count)!
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("appCell", forIndexPath: indexPath)
        let currentItem: String = (appDel.dataFetcher?.appTitles?[indexPath.row])!

        if (self.resultSearchController.active) {
            cell.textLabel?.text = filteredTableData[indexPath.row]
            return cell
        }
        else {
            cell.textLabel?.text = currentItem
            cell.detailTextLabel?.text = String(indexPath.row)
            return cell
        }


    }

}
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • It is not a recommended to add switch beneath searchbar like you want to. Add switch to navigation bar and control filter for tableview from there. Otherwise if you really want to, then add switch in header view of table. – NightFury Aug 13 '16 at 15:58

1 Answers1

0

Create a table header view by dragging a UIView to the top of the tableView, just above the first prototype cell. See this question for an example.

There you can place your UISwitchView and create an outlet to your VC.

Community
  • 1
  • 1
realtimez
  • 2,525
  • 2
  • 20
  • 24