我想知道如何在此表格视图中添加搜索栏,以便过滤掉数据,但仍会推送到我的详细视图控制器。如果有人知道使用Swift添加搜索栏的代码,我很想知道。
这是代码。
import UIKit
class TableViewLemon: UITableViewController {
let CarMake = ["Apple","Google"]
let CarModel = ["Nasdaq","Nasdaq",]
let CarImage = ["AppleImage","GoogleImage",]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return CarMake.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CellLemon = tableView.dequeueReusableCellWithIdentifier("Cell") as! CellLemon
cell.cellTopLabel.text = CarMake[indexPath.row]
cell.cellBottom.text = CarModel[indexPath.row]
let imageName = UIImage(named: CarImage[indexPath.row])
cell.cellImage.image = imageName
// Configure the cell...
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "DetailView") {
let VC = segue.destinationViewController as! DetailViewLemon
if let indexpath = self.tableView.indexPathForSelectedRow {
let Make = CarMake[indexpath.row] as String
VC.sentData1 = Make
let Model = CarModel[indexpath.row] as String
VC.sentData2 = Model
let Image = CarImage[indexpath.row] as String
VC.sentData3 = Image
}
}
}
}
答案 0 :(得分:1)
var requestList = [Request]()
var requestListConst = [Request]()
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
requestList = searchText.isEmpty ? requestListConst : requestListConst.filter({(dataString: Request) -> Bool in
return (dataString.customerTitle.range(of: searchText) != nil)
})
tableView.reloadData()
}
此代码对您有所帮助
这段代码就是例子 如果这段代码对你没有帮助 在用代码编写代码之前告诉我
答案 1 :(得分:0)
这可以帮到你: http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
这是iOS 8指南,但它也适用于iOS 9。
答案 2 :(得分:0)
您应该查看UISearchController
类,该类完全用于搜索数据并更新关联的视图控制器:
UISearchController对象基于与搜索栏的交互来管理搜索结果的显示。您可以将搜索控制器与现有视图控制器配合使用。如果您有一个具有可搜索内容的视图控制器,请将UISearchController对象的搜索栏合并到视图控制器的界面中。当用户与该搜索栏进行交互时,搜索控制器会自动显示一个新的视图控制器,其中包含您指定的搜索结果。
来自UISearchController class reference的介绍非常重要,因为它解释了当使用搜索栏时,您实际上会看到搜索控制器自动呈现的新视图控制器。
别担心,实现起来并不难:Apple提供great example一个带有搜索栏的tableview,同时在Objective-C和Swift上工作,你应该检查它。