簡體   English   中英

如何在Swift中實現表格視圖和搜索欄選擇的同步?

[英]How to have tableview and searchbar selection sync in Swift?

我有一個約有200個項目的表格視圖,允許用戶進行多個選擇,然后用戶提交他們的選擇。

我想在該tableview的標題中有一個搜索欄,這將使選擇過程更加容易。

我可以讓搜索結果表具有多個選擇,但是當用戶選擇並刪除其他選擇的查詢時,由於兩個都是不同的表,因此它們與表視圖不同步。

如何使用搜索功能實現多選表視圖。

例子-

我有帶有以下數據的表視圖-

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

我想要這樣的東西,當用戶在搜索欄中輸入“ sw”並選擇Swift並刪除搜索查詢以執行另一次搜索時。

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

現在,如果他/她輸入“ wh”並選擇“最新動態”,則表視圖選擇應為

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

當用戶點擊提交按鈕時,選擇數組應返回為[3,5]

有人可以幫忙嗎?

我是Swift和iOS開發的新手,我們將不勝感激。

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM