SearchView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. let emptyView = DIYEmptyView.empty(withImageStr: nil, titleStr: nil, detailStr: "没有历史搜索记录")
  16. emptyView!.contentViewY = kScaleValue(value: 60)
  17. collectionView.ly_emptyView = emptyView
  18. collectionView.ly_startLoading()
  19. setupData()
  20. }
  21. override func setupLayouts() {
  22. collectionView.snp.makeConstraints { (make) in
  23. make.edges.equalToSuperview()
  24. }
  25. }
  26. func setupData() {
  27. guard UserDefaults.standard.array(forKey: "SearchHistoryArray")?.isEmpty ?? true else {
  28. searchHistoryArray = UserDefaults.standard.array(forKey: "SearchHistoryArray") as! [String]
  29. collectionView.reloadData()
  30. return
  31. }
  32. }
  33. //MARK: - lazy
  34. private lazy var collectionView: UICollectionView = {
  35. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  36. collectionView.backgroundColor = UIColor.white
  37. collectionView.dataSource = self
  38. collectionView.delegate = self
  39. return collectionView
  40. }()
  41. private lazy var collectionViewLayout: UICollectionViewLeftAlignedLayout = {
  42. let collectionViewLayout = UICollectionViewLeftAlignedLayout.init()
  43. collectionViewLayout.minimumLineSpacing = 10
  44. collectionViewLayout.minimumInteritemSpacing = 10
  45. collectionViewLayout.sectionInset = UIEdgeInsets(top: 10, left: 14, bottom: 10, right: 14)
  46. collectionViewLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
  47. collectionViewLayout.estimatedItemSize = CGSize(width: ((kScreenWidth - 28) - 4*10)/5, height: 28)
  48. return collectionViewLayout
  49. }()
  50. }
  51. extension SearchView : UICollectionViewDelegateFlowLayout ,UICollectionViewDataSource {
  52. func numberOfSections(in collectionView: UICollectionView) -> Int {
  53. return 1
  54. }
  55. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  56. return searchHistoryArray.count
  57. }
  58. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  59. let cell = SearchCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  60. cell.hotArray = searchHistoryArray
  61. return cell
  62. }
  63. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  64. if self.searchCollectionViewDidSelectBlock != nil {
  65. self.searchCollectionViewDidSelectBlock!(indexPath)
  66. }
  67. }
  68. // 返回HeadView的宽高
  69. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  70. return CGSize(width: kScreenWidth, height: 41)
  71. }
  72. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  73. if kind == UICollectionView.elementKindSectionHeader
  74. {
  75. let searchHeaderCollectionReusableView = SearchHeaderCollectionReusableView.headerWith(collectionView: collectionView, kind: UICollectionView.elementKindSectionHeader, indexPath: indexPath)
  76. return searchHeaderCollectionReusableView
  77. }else {
  78. return UICollectionReusableView()
  79. }
  80. }
  81. }