首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用pull-to-refresh - swift下移背景视图

使用pull-to-refresh - swift下移背景视图
EN

Stack Overflow用户
提问于 2018-08-26 01:34:37
回答 1查看 480关注 0票数 0

我有一个带UIRefreshControl的UICollectionView,允许拉取刷新。当没有结果时,我为我的集合视图设置一个自定义的背景视图,如下所示:

collectionView?.backgroundView = someView (如here所述)

但是,当我拉入刷新时,backgroundView不会随collectionView一起下移。

我怎样才能在我的collectionView中有一个自定义的全尺寸视图,它可以使用refreshControl提供的拉取刷新功能进行下拉?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-06 16:09:27

在我看来,与其将someView赋值给collectionView?.backgroundView,不如让someView成为一个单元格。当您的collectionView数据源为空时,在collectionView上将此单元格显示为全尺寸单元格。

代码语言:javascript
复制
import UIKit

class EmptyCell: UICollectionViewCell {
  override init(frame: CGRect) {
    super.init(frame: frame)

    let backgroundImage = UIImageView.init(frame: frame)
    backgroundImage.image = UIImage.init(named: "bg")
    backgroundImage.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    self.contentView.addSubview(backgroundImage)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

  @IBOutlet weak var collectionView: UICollectionView!

  var shouldShowEmptyCell = false

  let refreshControl: UIRefreshControl = {
    let refreshControl: UIRefreshControl = UIRefreshControl.init()
    refreshControl.addTarget(self, action: #selector(refreshCollectionView(_:)), for: .valueChanged)
    return refreshControl;
  }()

  override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.register(EmptyCell.self, forCellWithReuseIdentifier: "EmptyCell")
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "NormalCell")

    addRefreshControl()
  }

  func addRefreshControl() -> Void {
    if #available(iOS 10.0, *) {
      collectionView.refreshControl = self.refreshControl
    } else {
      collectionView.addSubview(self.refreshControl)
    }
  }

  @objc private func refreshCollectionView(_ sender: Any) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
      self.shouldShowEmptyCell = !self.shouldShowEmptyCell
      self.collectionView.reloadData()
      self.refreshControl.endRefreshing()
    }
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return shouldShowEmptyCell ? 1 : 20;
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if shouldShowEmptyCell {
      return collectionView.dequeueReusableCell(withReuseIdentifier: "EmptyCell", for: indexPath)
    }

    let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NormalCell", for: indexPath)
    if indexPath.row % 3 == 0 {
      cell.backgroundColor = .red
    } else if indexPath.row % 3 == 1 {
      cell.backgroundColor = .blue
    } else if indexPath.row % 3 == 2 {
      cell.backgroundColor = .green
    }
    cell.layer.cornerRadius = 10
    return cell
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 8;
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 8;
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if shouldShowEmptyCell {
      return collectionView.bounds.size
    }

    return CGSize.init(width: collectionView.bounds.width / 2 - 4, height: collectionView.bounds.width / 2 - 4)
  }
}

有关更多详细信息,可以查看我的样例项目https://github.com/trungducc/stackoverflow/tree/empty-collection-view-pull-to-refresh

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52019697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档