我有一个 UIViewController,其中有一个 UITableView,在该 tableview 中我有多个部分,其中有一些项目,我必须在该 tableview 中搜索项目名称。我已经在我的 View Controller 中声明了这一点
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = UIColor(red: 39.0/255.0, green: 203.0/255.0, blue: 192.0/255.0, alpha: 1.0)
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.placeholder = "Search item"
var sections = [Category A, Category B, Category C]
var itemsA = [["Item": "item A","ItemId" : "1"],["Item": "itemB","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
var itemsB = [["Item": "item A","ItemId" : "1"],["Item": "itemB","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
var itemsC = [["Item": "item A","ItemId" : "1"],["Item": "itemB","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
}
func numberOfSections(in tableView: UITableView) -> Int {
if searchController.isActive {
return 1
}
return self.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive {
return filteredShops.count
}
switch (section) {
case 0:
return itemsA.count
case 1:
return itemsB.count
default:
return itemsC.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StoreCell") as! UITableViewCell
if searchController.isActive {
let filter = filteredShops[indexPath.row]
cell.storeName.text = filter["itemName"]
}
switch (indexPath.section) {
case 0:
//Access itemsA[indexPath.row]
case 1:
//Access itemsB[indexPath.row]
default:
//Access itemsC[indexPath.row]
}
return cell
}
func filteredshops(_ searchText: String, scope: String = "All") {
let filteredShopsA = storeLists.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
let filteredShopsB = medicalLists.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
let filteredShopsC = restaurantsLists.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
filteredShops = filteredShopsA + filteredShopsB + filteredShopsC
print(filteredShops)
tableView.reloadData()
}
我有 SearchResultUpdate 的扩展
extension NearByShopVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filteredshops(searchController.searchBar.text!)
}
}
我已经引用了这个问题,但没有找到任何解决方案 How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to search a UITableView with Swift
我尝试在没有上述问题的部分的情况下也没有显示正确的结果。谢谢!!
最佳答案
一种方法是当您过滤单个部分的工作时,首先像这样更改您的 filteredshops。
func filteredshops(_ searchText: String, scope: String = "All") {
let filteredShopsA = itemA.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
let filteredShopsB = itemB.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
let filteredShopsC = itemC.filter({ item in
if let name = item["itemName"], let query = searchController.searchBar.text {
return name.range(of: query, options: [.caseInsensitive, .diacriticInsensitive]) != nil
}
return false
})
filteredShops = filteredShopsA + filteredShopsB + filteredShopsC
tableView.reloadData()
}
现在使用 tableView 方法以这种方式进行更改。
func numberOfSections(in tableView: UITableView) -> Int {
if searchController.isActive {
return 1
}
return self.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive {
return filteredShops.count
}
switch (section) {
case 0:
return itemsA.count
case 1:
return itemsB.count
default:
return itemsC.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StoreCell") as! UITableViewCell
if searchController.isActive {
//Access filteredShops array
}
else {
switch (indexPath.section) {
case 0:
//Access itemsA[indexPath.row]
case 1:
//Access itemsB[indexPath.row]
default:
//Access itemsC[indexPath.row]
}
}
return cell
}
关于ios - 在 UITableView 中搜索具有多个部分的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609615/
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我有一个rubyonrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素