UIViewController 内带有 UITableView 的搜索栏
Posted
技术标签:
【中文标题】UIViewController 内带有 UITableView 的搜索栏【英文标题】:Search bar with UITableView inside UIViewController 【发布时间】:2016-04-26 08:52:24 【问题描述】:我正在尝试在我的 json 文件结果中创建搜索过滤器。
所以我有以下代码
import UIKit
import Alamofire
import SwiftyJSON
class checkViewController: UIViewController, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var checkTable: UITableView!
var arrRes = [[String:AnyObject]]()
var filteredData = [[String:AnyObject]]()
override func viewDidLoad()
super.viewDidLoad()
checkTable.dataSource = self
searchBar.delegate = self
filteredData = arrRes
Alamofire.request(.GET, "https://example.com/search", parameters: ["q": "", "type": "name"])
.responseJSON (responseData) -> Void in
if((responseData.result.value) != nil)
let swiftyJsonVar = JSON(responseData.result.value!)
print(swiftyJsonVar)
if let resData = swiftyJsonVar["data"].arrayObject
self.arrRes = resData as! [[String:AnyObject]]
if self.arrRes.count > 0
self.checkTable.reloadData()
func numberOfSectionsInTableView(tableView: UITableView) -> Int
return 1
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return arrRes.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("checkInCellView", forIndexPath: indexPath)
var dict = arrRes[indexPath.row]
cell.textLabel?.text = dict["name"] as? String
return cell
我正在尝试在我的班级中插入UISearchResultsUpdating
,但我收到一个不是成员的错误。所以我正在尝试UISearchBarDelegate
,但我只能在 [String] 数组中找到一种方法,而不是在这个示例中找到 [[String:AnyObject]]
class ViewController: UIViewController, UITableViewDataSource, UISearchBarDelegate
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
let data = ["New York, NY", "Los Angeles, CA", "Chicago, IL", "Houston, TX",
"Philadelphia, PA", "Phoenix, AZ", "San Diego, CA", "San Antonio, TX",
"Dallas, TX", "Detroit, MI", "San Jose, CA", "Indianapolis, IN",
"Jacksonville, FL", "San Francisco, CA", "Columbus, OH", "Austin, TX",
"Memphis, TN", "Baltimore, MD", "Charlotte, ND", "Fort Worth, TX"]
var filteredData: [String]!
override func viewDidLoad()
super.viewDidLoad()
tableView.dataSource = self
searchBar.delegate = self
filteredData = data
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("TableCell") as UITableViewCell
cell.textLabel?.text = filteredData[indexPath.row]
return cell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return filteredData.count
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
filteredData = searchText.isEmpty ? data : data.filter((dataString: String) -> Bool in
return dataString.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil
)
// This method updates filteredData based on the text in the Search Box
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
// When there is no text, filteredData is the same as the original data
if searchText.isEmpty
filteredData = data
else
// The user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
filteredData = data.filter((dataItem: String) -> Bool in
// If dataItem matches the searchText, return true to include it
if dataItem.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil
return true
else
return false
)
tableView.reloadData()
还有什么方法可以使它也适用于 [[String:AnyObject]] ???
因为如果我尝试这样做,我会得到一个错误
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
filteredData = searchText.isEmpty ? arrRes : arrRes.filter((dataString: [String:AnyObject]) -> Bool in
return dataString.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil
)
“[String : AnyObject]”类型的值没有成员“rangeOfString”
知道如何处理吗?
【问题讨论】:
【参考方案1】:希望你能从下面的代码中得到灵感。
override func viewDidLoad()
super.viewDidLoad()
var MyStringArray: [String] = ["ABC","XYZ","SomeData"]
let strDemo : String = "SomeData"
for indexNo in 0..<MyStringArray.count
if MyStringArray[indexNo].rangeOfString(strDemo) != nil
print("found")
else
print("Not found")
【讨论】:
以上是关于UIViewController 内带有 UITableView 的搜索栏的主要内容,如果未能解决你的问题,请参考以下文章
将带有 ScrollView 和 AutoLayout 的 UIViewController 添加到另一个 UIViewController
UIViewController 没有带有 UIModalPresentationFormSheet 的 parentViewController?