我的桌面视图上方有一个搜索栏。表视图中的数据来自我的服务。我正在尝试对表视图数据应用搜索过滤器。我尝试了一些代码,但它没有用。我的搜索栏代码就是这个,
UIViewController,UISearchBarDelegate,UITextFieldDelegate,UITextViewDelegate,ShowsAlert
@IBOutlet weak var searchBar: UISearchBar!
var filteredData = [String]()
var isSearching = false
var dishNameArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
menuView.isHidden = true
reviewView.isHidden = true
infoView.isHidden = true
scrollView.isScrollEnabled = false
//TableView Delegates
menuTableView.delegate = self
menuTableView.dataSource = self
reviewTableView.delegate = self
reviewTableView.dataSource = self
reviewTableView.reloadData()
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
segmentControl.tintColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
searchBar.delegate = self
dishNameLbl.text = name
dishDescripLbl.text = resDesc
minOrderLbl.text = minOrder
deliveryLbl.text = deliveryTime
}
private func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
isSearching = true
}
private func searchBarTextDidEndEditing(searchBar: UISearchBar) {
isSearching = false
}
private func searchBarCancelButtonClicked(searchBar: UISearchBar) {
isSearching = false
}
private func searchBarSearchButtonClicked(searchBar: UISearchBar) {
isSearching = false
}
private func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
guard let searchText = searchBar.text else {
isSearching = false
return
}
filteredData = dishNameArray.filter({
return $0.lowercased().contains(searchText.lowercased())
})
isSearching = filteredData.count > 0
self.menuTableView.reloadData()
}
extension RestaurantMenuVC: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == menuTableView{
if isSearching{
return filteredData.count
}
return ResMenuService.instance.categoryModelInstance.count
}
else{
return AllReviewsService.instance.allReviewsModel.count
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == menuTableView{
return 57
}
else{
return 137
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == menuTableView{
let cell = menuTableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! RestaurantMenuTableViewCell
if isSearching{
cell.dishTitleLbl.text = filteredData[indexPath.row]
dishNameArray.append(cell.dishTitleLbl.text!)
}
// let cell = menuTableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! RestaurantMenuTableViewCell
cell.dishTitleLbl.text = ResMenuService.instance.categoryModelInstance[indexPath.row].categoryName
cell.cardView.layer.cornerRadius = 5
cell.selectionStyle = .none
return cell
}
else
{
let cell = reviewTableView.dequeueReusableCell(withIdentifier: "reviewCell", for: indexPath) as! AllReviewsTableViewCell
cell.nameLbl.text = AllReviewsService.instance.allReviewsModel[indexPath.row].name
cell.descriptionLbl.text = AllReviewsService.instance.allReviewsModel[indexPath.row].description
cell.timeLbl.text = AllReviewsService.instance.allReviewsModel[indexPath.row].time
cell.ratingView.rating = Double(AllReviewsService.instance.allReviewsModel[indexPath.row].rating)
cell.backgroundColor = UIColor.clear
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == menuTableView{
let minimumSpending = String(ResMenuService.instance.restaurntDetailModelInstance[indexPath.row].minimumSpending)
UserDefaults.standard.set(minimumSpending, forKey: "minimumSpending")
UserDefaults.standard.synchronize()
let categoryModel = ResMenuService.instance.categoryModelInstance
let subCategoryModel = ResMenuService.instance.categoryModelInstance[indexPath.row].subCategories
let AddonCategoryModel = ResMenuService.instance.categoryModelInstance[indexPath.row].subCategories[0].items[0].addonCategory
// if categoryId == subCategoryId{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RestaurantMenuDetailVC") as! RestaurantMenuDetailVC
vc.categoryModel = categoryModel
vc.subCategoryModel = subCategoryModel
vc.AddonCategoryModel = AddonCategoryModel
self.navigationController?.pushViewController(vc, animated: true)
//}
}
else{
print("Hello")
}
}
}
但是当我输入内容时,它不会过滤数据。这是我的模型类,
struct RestaurantDetailModel {
public private(set) var restaurantId:String!
public private(set) var shopLat:String!
public private(set) var shopLng:String!
public private(set) var street:String!
public private(set) var town:String!
public private(set) var zipCode:String!
public private(set) var cellNo:String!
public private(set) var landLine:Int!
public private(set) var shopName:String!
public private(set) var deliveryTime:Int!
public private(set) var collectionTime:Int!
public private(set) var facebookLink:String!
public private(set) var twitterLink:String!
public private(set) var googleLink:String!
public private(set) var instagramLink:String!
public private(set) var pinterestLink:String!
public private(set) var address:String!
public private(set) var preorderPref:String!
public private(set) var orderStatus:Bool!
public private(set) var minimumSpending:Int!
public private(set) var restaurantTimings:[RestaurantTimingsModel]!
}
答案 0 :(得分:0)
试试这个
mydf$timezone <- gsub("^.*\\s", "", format(mydf$myvector, usetz = TRUE))
head(mydf)
# myvector timezone
#1 2018-10-01 00:00:00 CEST
#2 2018-10-01 01:00:00 CEST
#3 2018-10-01 02:00:00 CEST
#4 2018-10-01 03:00:00 CEST
#5 2018-10-01 04:00:00 CEST
#6 2018-10-01 05:00:00 CEST
答案 1 :(得分:0)
你没有在任何地方将isSearching设置为true。所以总是错误。
因此,在设置isSearching = true
func updateSearchResults(for searchController: UISearchController)
是一个UISearchControllerDelegate方法。由于您没有使用UISearchController,因此在您的情况下不会调用它。您需要使用UISearchBarDelegate函数。
尝试以下更改。 Reference used
在viewDidLoad中添加以下行:
searchBar.delegate = self
添加以下功能:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
isSearching = true
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
isSearching = false
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
isSearching = false
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
isSearching = false
}
将updateSearchResults函数更改为:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
guard let searchText = searchBar.text else {
isSearching = false
return
}
filteredData = dishNameArray.filter({
return $0.lowercased().contains(searchText.lowercased())
})
isSearching = filteredData.count > 0
self.menuTableView.reloadData()
}
您还需要使ViewController符合UISearchBarDelegate
所以添加它,如下所示:
class ViewController: UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate
使用UISearchController时更容易。请参阅下面的示例。
使用UISearchController的示例:
我刚刚将这个例子放在一个操场上,该操场显示了如何使用UISearchController执行此操作。
import UIKit
import PlaygroundSupport
class ViewController: UITableViewController {
let searchController = UISearchController(searchResultsController: nil)
var names = [
"John",
"Terry",
"Martin",
"Steven",
"Michael",
"Thomas",
"Jason",
"Matthew"
]
var filteredNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Search Example"
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
return filteredNames.count
} else {
return names.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell() // don't do this, i am for example.
var name: String
if isFiltering() {
name = filteredNames[indexPath.row]
} else {
name = names[indexPath.row]
}
cell.textLabel?.text = name
return cell
}
func searchBarIsEmpty() -> Bool {
// Returns true if the text is empty or nil
return searchController.searchBar.text?.isEmpty ?? true
}
func isFiltering() -> Bool {
return searchController.isActive && !searchBarIsEmpty()
}
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
filteredNames = names.filter({( name : String) -> Bool in
return name.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
}
extension ViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
}
let vc = ViewController()
let nav = UINavigationController()
nav.viewControllers = [vc]
PlaygroundPage.current.liveView = nav
编辑2:使用SearchBar
import UIKit
import PlaygroundSupport
class ViewController: UITableViewController, UISearchBarDelegate {
var searchBar: UISearchBar!
var isFiltering = false
var names = [
"John",
"Terry",
"Martin",
"Steven",
"Michael",
"Thomas",
"Jason",
"Matthew"
]
var filteredNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Search Example"
searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
searchBar.delegate = self
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
tableView.tableHeaderView?.addSubview(searchBar)
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
isFiltering = true
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard let searchText = searchBar.text else {
isFiltering = false
return
}
filteredNames = names.filter({
return $0.lowercased().contains(searchText.lowercased())
})
isFiltering = filteredNames.count > 0
self.tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering {
return filteredNames.count
} else {
return names.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell() // don't do this, i am for example.
var name: String
if isFiltering {
name = filteredNames[indexPath.row]
} else {
name = names[indexPath.row]
}
cell.textLabel?.text = name
return cell
}
}
let vc = ViewController()
PlaygroundPage.current.liveView = vc
编辑3:提供新信息
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard let searchText = searchBar.text else {
isFiltering = false
return
}
filteredData = ResMenuService.instance.categoryModelInstance.filter({
return $0.categoryName.lowercased().contains(searchText.lowercased())
})
isFiltering = filteredData.count > 0
self.tableView.reloadData()
}
答案 2 :(得分:0)
您需要添加功能以识别搜索状态,如下所示:
// case class defintions here
// regex pattern_1, pattern_2, pattern_3 defined here
val rdd = sc.textFile("file").cache
// Filter in 3 rdds based on the pattern that gets matched
val receivedRdd = rdd.filter(_.matches(pattern_1.toString)).map(pattern_1.unapplySeq(_).get)
val sentRdd = rdd.filter(_.matches(pattern_3.toString)).map(pattern_3.unapplySeq(_).get)
val otherRdd = rdd.filter(_.matches(pattern_2.toString)).map(pattern_2.unapplySeq(_).get)
// Convert it to a dataframe
// Names are matching with case class Rlog and Slog
// To facilitate the conversion to Datasets
val receivedDF = receivedRdd.map{ case List(a,b,c,d,e,f) => (a,b,c,d,e,f)}
.toDF("dateTime" , "server_name", "log_type", "server_addr", "action", "target_addr")
val sentDF = sentRdd.map{ case List(a,b,c,d,e,f) => (a,b,c,d,e,f)}
.toDF("dateTime" , "server_name", "log_type", "server_addr", "action", "target_addr")
// Convert multiple lines containing msg-type, content etc to single line using pivot
val otherDF = otherRdd.map{ case List(ts , srvr, typ, i1 , i2) => (ts , srvr, typ, i1 , i2) }
.toDF("dateTime" , "server_name", "log_type", "i1" , "i2")
.groupBy("dateTime" , "server_name", "log_type")
.pivot("i1").agg(first($"i2") )
.select($"dateTime", $"server_name", $"log_type", $"sec".as("cost") , $"Msg-Type".as("msg_type"), $"Content".as("content"))
otherDF.show
//+-------------------+-----------+--------+----+--------+----------+
//| dateTime|server_name|log_type|cost|msg_type| content|
//+-------------------+-----------+--------+----+--------+----------+
//|2018-04-11 06:27:34| localhost| debug| 0.3| text|XXXXXXXXXX|
//|2018-04-11 06:27:36| localhost| debug| 0.4| text|XXXXXXXXXX|
//+-------------------+-----------+--------+----+--------+----------+
// Finally join based on dateTime, server_name and log_type and convert to Datasets
val RlogDS = receivedDF.join(otherDF, Seq("dateTime" , "server_name", "log_type")).as[Rlog]
val SlogDS = sentDF.join(otherDF, Seq("dateTime" , "server_name", "log_type")).as[Slog]
RlogDS.show(false)
//+-------------------+-----------+--------+-----------+--------+-----------+----+--------+----------+
//| dateTime|server_name|log_type|server_addr| action|target_addr|cost|msg_type| content|
//+-------------------+-----------+--------+-----------+--------+-----------+----+--------+----------+
//|2018-04-11 06:27:36| localhost| debug| localhost|discover| 0.0.0.0| 0.4| text|XXXXXXXXXX|
//+-------------------+-----------+--------+-----------+--------+-----------+----+--------+----------+
SlogDS.show(false)
//+-------------------+-----------+--------+-----------+--------+-----------+----+--------+----------+
//|dateTime |server_name|log_type|server_addr|action |target_addr|cost|msg_type|content |
//+-------------------+-----------+--------+-----------+--------+-----------+----+--------+----------+
//|2018-04-11 06:27:34|localhost |debug |localhost |response|0.0.0.0 |0.3 |text |XXXXXXXXXX|
//+-------------------+-----------+--------+-----------+--------+-----------+----+--------+----------