IOS:类似于邮件或信息应用的可滚动搜索栏
IOS: Scrollable search bar similar to Mail or Messages apps
新 iOS 开发人员在这里!我喜欢向上滚动到隐藏搜索栏的设计。我想复制它,但很多解决方案似乎已经过时,所以我真的很困惑。
iPhone: Hide UITableView search bar by default
从这里开始,好像有 2 个步骤。
- 将搜索栏添加到可滚动 table 视图
使用
添加代码,使其在应用加载时(在 viewdidload 中?)滚动到第二行
yourTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
我无法通过#1。我尝试在 3 个位置放置搜索栏...
- A) 如果我将它放在此处(在页眉顶部?),它会按预期固定在顶部,但不可滚动。
- B) 如果我把它放在这里,它仍然不可滚动并且它会阻塞第一行
- C) 如果我把它放在这里,它会按预期在行中重复。
不知何故我需要把它放在 table 上而不复制,所以我对此感到困惑。
所以我认为您必须在代码中执行此操作。如果你想这样做,我按照这里的教程进行操作并且成功了。
http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
添加 UISearchResultsUpdating
class TableViewController: UITableViewController, UISearchResultsUpdating {
添加这些属性...
let tableData = ["One","Two","Three","Twenty-One", "vasdf", "vasdfd", "3343", "ad23", "454d", "vasdf32", "va343", "vasdf3r2", "vasdfd2", "vasr52f", "awefwr32vwf"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()
在此处添加self.resultSearchController...
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
}
注意self.resultSearchController.这些函数中的活动条件...
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 2
if (self.resultSearchController.active) {
return self.filteredTableData.count
}
else {
return self.tableData.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// 3
if (self.resultSearchController.active) {
cell.textLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
}
然后在这里做搜索代码
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text as String!)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
我想就是这样。祝你好运!
新 iOS 开发人员在这里!我喜欢向上滚动到隐藏搜索栏的设计。我想复制它,但很多解决方案似乎已经过时,所以我真的很困惑。
iPhone: Hide UITableView search bar by default
从这里开始,好像有 2 个步骤。
- 将搜索栏添加到可滚动 table 视图
使用
添加代码,使其在应用加载时(在 viewdidload 中?)滚动到第二行yourTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
我无法通过#1。我尝试在 3 个位置放置搜索栏...
- A) 如果我将它放在此处(在页眉顶部?),它会按预期固定在顶部,但不可滚动。
- B) 如果我把它放在这里,它仍然不可滚动并且它会阻塞第一行
- C) 如果我把它放在这里,它会按预期在行中重复。
不知何故我需要把它放在 table 上而不复制,所以我对此感到困惑。
所以我认为您必须在代码中执行此操作。如果你想这样做,我按照这里的教程进行操作并且成功了。
http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
添加 UISearchResultsUpdating
class TableViewController: UITableViewController, UISearchResultsUpdating {
添加这些属性...
let tableData = ["One","Two","Three","Twenty-One", "vasdf", "vasdfd", "3343", "ad23", "454d", "vasdf32", "va343", "vasdf3r2", "vasdfd2", "vasr52f", "awefwr32vwf"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()
在此处添加self.resultSearchController...
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
}
注意self.resultSearchController.这些函数中的活动条件...
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 2
if (self.resultSearchController.active) {
return self.filteredTableData.count
}
else {
return self.tableData.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// 3
if (self.resultSearchController.active) {
cell.textLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
}
然后在这里做搜索代码
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text as String!)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
我想就是这样。祝你好运!