123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // SearchView.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/4/23.
- // Copyright © 2019 南鑫林. All rights reserved.
- //
- import UIKit
- class SearchView: BaseView {
-
- typealias SearchCollectionViewDidSelectBlock = (_ indexPath: IndexPath) -> Void
- var searchCollectionViewDidSelectBlock : SearchCollectionViewDidSelectBlock?
- var searchHistoryArray = Array<String>()
- override func setupViews() {
- addSubview(collectionView)
- setupData()
- }
-
- override func setupLayouts() {
- collectionView.snp.makeConstraints { (make) in
- make.edges.equalToSuperview()
- }
- }
-
- override func setupData() {
-
- let emptyView = DIYEmptyView.empty(withImageStr: nil, titleStr: nil, detailStr: "没有历史搜索记录")
- emptyView!.contentViewY = kScaleValue(value: 60)
- collectionView.ly_emptyView = emptyView
- collectionView.ly_startLoading()
-
- guard UserDefaults.standard.array(forKey: "SearchHistoryArray")?.isEmpty ?? true else {
- searchHistoryArray = UserDefaults.standard.array(forKey: "SearchHistoryArray") as! [String]
- collectionView.reloadData()
- collectionView.ly_endLoading()
- return
- }
- }
-
-
- //MARK: - lazy
- private lazy var collectionView: UICollectionView = {
- let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
- collectionView.backgroundColor = UIColor.white
- collectionView.dataSource = self
- collectionView.delegate = self
- return collectionView
- }()
-
- private lazy var collectionViewLayout: UICollectionViewLeftAlignedLayout = {
- let collectionViewLayout = UICollectionViewLeftAlignedLayout.init()
- collectionViewLayout.minimumLineSpacing = 10
- collectionViewLayout.minimumInteritemSpacing = 10
- collectionViewLayout.sectionInset = UIEdgeInsets(top: 10, left: 14, bottom: 10, right: 14)
- collectionViewLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
- collectionViewLayout.estimatedItemSize = CGSize(width: ((kScreenWidth - 28) - 4*10)/5, height: 28)
- return collectionViewLayout
- }()
-
- }
- extension SearchView : UICollectionViewDelegateFlowLayout ,UICollectionViewDataSource {
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
- }
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return searchHistoryArray.prefix(10).count
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = SearchCollectionViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
- cell.hotArray = searchHistoryArray
- return cell
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if self.searchCollectionViewDidSelectBlock != nil {
- self.searchCollectionViewDidSelectBlock!(indexPath)
- }
- }
-
- // 返回HeadView的宽高
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
- return CGSize(width: kScreenWidth, height: 41)
-
- }
-
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
- if kind == UICollectionView.elementKindSectionHeader
- {
- let searchHeaderCollectionReusableView = SearchHeaderCollectionReusableView.headerWith(collectionView: collectionView, kind: UICollectionView.elementKindSectionHeader, indexPath: indexPath)
- searchHeaderCollectionReusableView.deleteButtonBlock = {
- [weak self] in
- UserDefaults.standard.removeObject(forKey: "SearchHistoryArray")
- self?.searchHistoryArray.removeAll()
- collectionView.reloadData()
- }
- return searchHeaderCollectionReusableView
- }else {
- return UICollectionReusableView()
- }
- }
-
- }
|