Kunal Verma
Kunal Verma

Reputation: 602

How to have tableview and searchbar selection sync in Swift?

I have a tableview with around 200 items which allows users to have multiple selections, after which user submits their selection.

I want to have a searchbar in the header of that tableview which will make the selection process easier.

I can have the search results table to have multiple selection but when user selects and removes the query for other the selections don't sync with the tableview as both are different tables.

How can I achieve a multiple selection table view with search functionality.

Example --

I have table view with the following data -

__ Hello
__ Hello World
__ hi There
__ What's Up
__ iOS 9 dev
__ Swift
__ Hey there

i wanna have something like this, when the user enters "sw" in the search bar and selects Swift and deletes the search query to perform another search.

__ Hello
__ Hello World
__ hi There
__ What's Up
__ iOS 9 dev
√ Swift
__ Hey there

and now if he/she writes "wh" and selects What's Up, the table view selection should be

__ Hello
__ Hello World
__ hi There
√  What's Up
__ iOS 9 dev
√  Swift
__ Hey there

And when the user taps a submit button then the selection array should be returned as [3,5]

Can anyone help in this?

I'm fairly new to swift and iOS development and help would be greatly appreciated.

Upvotes: 0

Views: 1742

Answers (1)

Claudio Castro
Claudio Castro

Reputation: 1569

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {

@IBOutlet var tableView: UITableView!

@IBOutlet var searchBar : UISearchBar!


// third field in array is your index, everything more easy with it

var array = [["Swift", false, 0], ["teste", false, 1], ["linguagem", false, 2], ["objectivec", false, 3]]
var arrayFilter = []


@IBOutlet weak var okbutton: UIButton!


@IBAction func okButton(sender: AnyObject) {
    var selection : [Int] = []
    for element in self.array {
        if element[1] as! Bool {
            selection.append(element[2] as! Int)
        }
    }
    print(selection)
}



func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

// filter array with string that start with searchText

    self.arrayFilter = array.filter{
        if let pos = ($0[0] as! String).lowercaseString.rangeOfString(searchText.lowercaseString) {
            return (pos.startIndex == ($0[0] as! String).startIndex)
        }
        return false
    }
    tableView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.searchBar.text == "" ? self.array.count : self.arrayFilter.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    let lista = self.searchBar.text == "" ? self.array : self.arrayFilter
    cell.textLabel?.text = lista[indexPath.row][0] as? String

// check cell based on second field

    cell.accessoryType = lista[indexPath.row][1] as! Bool ? .Checkmark : .None
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let lista = self.searchBar.text == "" ? self.array : self.arrayFilter
    let id = lista[indexPath.row][2] as! Int

// invert the value of checked
    self.array[id][1] = !(array[id][1] as! Bool)
    self.searchBar.text = ""
    tableView.reloadData()
}

}

Upvotes: 2

Related Questions