SearchView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // SearchView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/4/23.
  6. // Copyright © 2019 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. class SearchView: BaseView {
  10. typealias SearchCollectionViewDidSelectBlock = (_ indexPath: IndexPath) -> Void
  11. var searchCollectionViewDidSelectBlock : SearchCollectionViewDidSelectBlock?
  12. var searchHistoryArray = Array<String>()
  13. override func setupViews() {
  14. addSubview(collectionView)
  15. setupData()
  16. }
  17. override func setupLayouts() {
  18. collectionView.snp.makeConstraints { (make) in
  19. make.edges.equalToSuperview()
  20. }
  21. }
  22. override func setupData() {
  23. let emptyView = DIYEmptyView.empty(withImageStr: nil, titleStr: nil, detailStr: "没有历史搜索记录")
  24. emptyView!.contentViewY = kScaleValue(value: 60)
  25. collectionView.ly_emptyView = emptyView
  26. collectionView.ly_startLoading()
  27. guard UserDefaults.standard.array(forKey: "SearchHistoryArray")?.isEmpty ?? true else {
  28. searchHistoryArray = UserDefaults.standard.array(forKey: "SearchHistoryArray") as! [String]
  29. collectionView.reloadData()
  30. collectionView.ly_endLoading()
  31. return
  32. }
  33. }
  34. //MARK: - lazy
  35. private lazy var collectionView: UICollectionView = {
  36. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  37. collectionView.backgroundColor = UIColor.white
  38. collectionView.dataSource = self
  39. collectionView.delegate = self
  40. return collectionView
  41. }()
  42. private lazy var collectionViewLayout: UICollectionViewLeftAlignedLayout = {
  43. let collectionViewLayout = UICollectionViewLeftAlignedLayout.init()
  44. collectionViewLayout.minimumLineSpacing = 10
  45. collectionViewLayout.minimumInteritemSpacing = 10
  46. collectionViewLayout.sectionInset = UIEdgeInsets(top: 10, left: 14, bottom: 10, right: 14)
  47. collectionViewLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
  48. collectionViewLayout.estimatedItemSize = CGSize(width: ((kScreenWidth - 28) - 4*10)/5, height: 28)
  49. return collectionViewLayout
  50. }()
  51. }
  52. extension SearchView : UICollectionViewDelegateFlowLayout ,UICollectionViewDataSource {
  53. func numberOfSections(in collectionView: UICollectionView) -> Int {
  54. return 1
  55. }
  56. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  57. return searchHistoryArray.prefix(10).count
  58. }
  59. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  60. let cell = SearchCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  61. cell.hotArray = searchHistoryArray
  62. return cell
  63. }
  64. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  65. if self.searchCollectionViewDidSelectBlock != nil {
  66. self.searchCollectionViewDidSelectBlock!(indexPath)
  67. }
  68. }
  69. // 返回HeadView的宽高
  70. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  71. return CGSize(width: kScreenWidth, height: 41)
  72. }
  73. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  74. if kind == UICollectionView.elementKindSectionHeader
  75. {
  76. let searchHeaderCollectionReusableView = SearchHeaderCollectionReusableView.headerWith(collectionView: collectionView, kind: UICollectionView.elementKindSectionHeader, indexPath: indexPath)
  77. searchHeaderCollectionReusableView.deleteButtonBlock = {
  78. [weak self] in
  79. UserDefaults.standard.removeObject(forKey: "SearchHistoryArray")
  80. self?.searchHistoryArray.removeAll()
  81. collectionView.reloadData()
  82. }
  83. return searchHeaderCollectionReusableView
  84. }else {
  85. return UICollectionReusableView()
  86. }
  87. }
  88. }