Commit 9a5785db authored by Steven杜宇's avatar Steven杜宇

// 搜索

parent 3cbfa210
...@@ -10,6 +10,8 @@ import UIKit ...@@ -10,6 +10,8 @@ import UIKit
class YHSearchInfomationVC: YHBaseViewController { class YHSearchInfomationVC: YHBaseViewController {
static let searchInfoHistoryKey = "searchInfoHistoryKey"
static let searchHistoryMaxCount = 10
var items:[YHHomeListModel] = [] var items:[YHHomeListModel] = []
lazy var viewModel : YHHomePageViewModel = { lazy var viewModel : YHHomePageViewModel = {
...@@ -23,12 +25,16 @@ class YHSearchInfomationVC: YHBaseViewController { ...@@ -23,12 +25,16 @@ class YHSearchInfomationVC: YHBaseViewController {
[weak self] text in [weak self] text in
guard let self = self else { return } guard let self = self else { return }
searchText(self.searchBar.textField.text ?? "") searchText(self.searchBar.textField.text ?? "")
if let searchText = self.searchBar.textField.text {
self.saveSearchHistory(searchText)
}
} }
bar.textChange = { bar.textChange = {
[weak self] text in [weak self] text in
guard let self = self else { return } guard let self = self else { return }
if isEmptyString(text) { if isEmptyString(text) {
searchHistoryView.isHidden = false searchHistoryView.isHidden = false
searchHistoryView.updateDataSource(self.getSearchHistoryList())
items.removeAll() items.removeAll()
self.tableView.reloadData() self.tableView.reloadData()
} }
...@@ -57,6 +63,7 @@ class YHSearchInfomationVC: YHBaseViewController { ...@@ -57,6 +63,7 @@ class YHSearchInfomationVC: YHBaseViewController {
lazy var searchHistoryView: YHSearchInfoHistoryView = { lazy var searchHistoryView: YHSearchInfoHistoryView = {
let view = YHSearchInfoHistoryView(frame: CGRect(x: 0, y: searchBar.frame.maxY+8, width: KScreenWidth, height: KScreenHeight-searchBar.frame.maxY-8)) let view = YHSearchInfoHistoryView(frame: CGRect(x: 0, y: searchBar.frame.maxY+8, width: KScreenWidth, height: KScreenHeight-searchBar.frame.maxY-8))
view.updateDataSource(self.getSearchHistoryList())
view.selectBlock = { view.selectBlock = {
[weak self] text in [weak self] text in
guard let self = self else { return } guard let self = self else { return }
...@@ -138,6 +145,39 @@ class YHSearchInfomationVC: YHBaseViewController { ...@@ -138,6 +145,39 @@ class YHSearchInfomationVC: YHBaseViewController {
self.tableView.reloadData() self.tableView.reloadData()
} }
} }
func getSearchHistoryList() -> [String] {
if let arr = UserDefaults.standard.array(forKey: Self.searchInfoHistoryKey) as? [String] {
return arr
}
return []
}
func saveSearchHistory(_ text: String) {
var arr = getSearchHistoryList()
// 查找历史记录中是否有相同记录
var targetIndex = -1
for (index, history) in arr.enumerated() {
if history == text {
targetIndex = index
break
}
}
// 去除相同记录
if 0 <= targetIndex && targetIndex < arr.count {
arr.remove(at: targetIndex)
}
// 将最近记录放在第一位
if arr.count < Self.searchHistoryMaxCount {
arr.insert(text, at: 0)
} else {
arr.insert(text, at: 0)
arr.removeLast()
}
UserDefaults.standard.set(arr, forKey: Self.searchInfoHistoryKey)
UserDefaults.standard.synchronize()
}
} }
extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource { extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource {
......
...@@ -10,7 +10,7 @@ import UIKit ...@@ -10,7 +10,7 @@ import UIKit
class YHSearchInfoHistoryView: UIView { class YHSearchInfoHistoryView: UIView {
var historyItems:[String] = ["阿法守法发生懂法守法阿法守法发生懂法守法阿法守法发生懂法守法阿法守法发生懂法守法", "几天萨法", "阿德", "守法", "几天萨法", "德", "阿法守法发生懂法守法", "阿德", "几天", "阿德", "守法", "几天萨法"] var historyItems:[String] = []
// var historyItems:[String] = ["几天2345", "阿", "萨法", "阿德"] // var historyItems:[String] = ["几天2345", "阿", "萨法", "阿德"]
var selectBlock:((String)->())? var selectBlock:((String)->())?
...@@ -70,6 +70,12 @@ class YHSearchInfoHistoryView: UIView { ...@@ -70,6 +70,12 @@ class YHSearchInfoHistoryView: UIView {
} }
} }
func updateDataSource(_ arr:[String]) {
historyItems.removeAll()
historyItems.append(contentsOf: arr)
self.collectionView.reloadData()
}
// 计算文字宽度大小 // 计算文字宽度大小
func getwith(font: UIFont, height: CGFloat, string: String) -> CGSize { func getwith(font: UIFont, height: CGFloat, string: String) -> CGSize {
let size = CGSize.init(width: CGFloat(MAXFLOAT), height: height) let size = CGSize.init(width: CGFloat(MAXFLOAT), height: height)
......
...@@ -20,6 +20,7 @@ class YHSearchItemLayout: UICollectionViewFlowLayout { ...@@ -20,6 +20,7 @@ class YHSearchItemLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let arrCell = super.layoutAttributesForElements(in: rect) let arrCell = super.layoutAttributesForElements(in: rect)
if arrCell == nil { return nil } if arrCell == nil { return nil }
if arrCell!.count <= 1 { return arrCell }
for i in 1 ..< arrCell!.count { for i in 1 ..< arrCell!.count {
//当前 UICollectionViewLayoutAttributes //当前 UICollectionViewLayoutAttributes
let currentLayout = arrCell![i] let currentLayout = arrCell![i]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment