如何在 Swift 中同步表格视图和搜索栏选择?
How to have tableview and searchbar selection sync in Swift?
我有一个 table 视图,其中包含大约 200 个项目,允许用户进行多项选择,然后用户提交他们的选择。
我想在 table 视图的 header 中有一个搜索栏,这将使选择过程更容易。
我可以让搜索结果 table 有多项选择,但是当用户选择和删除其他查询时,选择不会与 table 视图同步,因为两者不同 tables.
如何实现具有搜索功能的多选 table 视图。
示例 --
我有 table 查看以下数据 -
__ 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
现在如果 he/she 写入 "wh" 并选择 What's Up,table 视图选择应该是
__ 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 = ([=10=][0] as! String).lowercaseString.rangeOfString(searchText.lowercaseString) {
return (pos.startIndex == ([=10=][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()
}
}
我有一个 table 视图,其中包含大约 200 个项目,允许用户进行多项选择,然后用户提交他们的选择。
我想在 table 视图的 header 中有一个搜索栏,这将使选择过程更容易。
我可以让搜索结果 table 有多项选择,但是当用户选择和删除其他查询时,选择不会与 table 视图同步,因为两者不同 tables.
如何实现具有搜索功能的多选 table 视图。
示例 --
我有 table 查看以下数据 -
__ 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
现在如果 he/she 写入 "wh" 并选择 What's Up,table 视图选择应该是
__ 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 = ([=10=][0] as! String).lowercaseString.rangeOfString(searchText.lowercaseString) {
return (pos.startIndex == ([=10=][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()
}
}