在 UICollectionView 页脚中显示活动指示器

Posted

技术标签:

【中文标题】在 UICollectionView 页脚中显示活动指示器【英文标题】:Show activity indicator in UICollectionView Footer 【发布时间】:2018-10-10 10:14:57 【问题描述】:

如何在集合视图页脚中显示和隐藏活动指示器。我需要在 collectionview 的页脚区域添加一个活动指示器。

【问题讨论】:

到目前为止你尝试过什么?什么不起作用? 【参考方案1】:

给你的样品:

import UIKit

open class PagingTableView: UITableView 

  private var loadingView: UIView!
  private var indicator: UIActivityIndicatorView!
  internal var page: Int = 0
  internal var previousItemCount: Int = 0

  open var currentPage: Int 
    get 
      return page
    
  

  open weak var pagingDelegate: PagingTableViewDelegate? 
    didSet 
      pagingDelegate?.paginate(self, to: page)
    
  

  open var isLoading: Bool = false 
    didSet 
      isLoading ? showLoading() : hideLoading()
    
  

  open func reset() 
    page = 0
    previousItemCount = 0
    pagingDelegate?.paginate(self, to: page)
  

  private func paginate(_ tableView: PagingTableView, forIndexAt indexPath: IndexPath) 
    let itemCount = tableView.dataSource?.tableView(tableView, numberOfRowsInSection: indexPath.section) ?? 0
    guard indexPath.row == itemCount - 1 else  return 
    guard previousItemCount != itemCount else  return 
    page += 1
    previousItemCount = itemCount
    pagingDelegate?.paginate(self, to: page)
  

  private func showLoading() 
    if loadingView == nil 
      createLoadingView()
    
    tableFooterView = loadingView
  

  private func hideLoading() 
    reloadData()
    pagingDelegate?.didPaginate?(self, to: page)
    tableFooterView = nil
  

  private func createLoadingView() 
    loadingView = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height: 50))
    indicator = UIActivityIndicatorView()
    indicator.color = UIColor.lightGray
    indicator.translatesAutoresizingMaskIntoConstraints = false
    indicator.startAnimating()
    loadingView.addSubview(indicator)
    centerIndicator()
    tableFooterView = loadingView
  

  private func centerIndicator() 
    let xCenterConstraint = NSLayoutConstraint(
      item: loadingView, attribute: .centerX, relatedBy: .equal,
      toItem: indicator, attribute: .centerX, multiplier: 1, constant: 0
    )
    loadingView.addConstraint(xCenterConstraint)

    let yCenterConstraint = NSLayoutConstraint(
      item: loadingView, attribute: .centerY, relatedBy: .equal,
      toItem: indicator, attribute: .centerY, multiplier: 1, constant: 0
    )
    loadingView.addConstraint(yCenterConstraint)
  

  override open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell 
    paginate(self, forIndexAt: indexPath)
    return super.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
  


【讨论】:

问题是关于UICollectionView 将当前逻辑移动到 UICollectionView 非常简单。 %)

以上是关于在 UICollectionView 页脚中显示活动指示器的主要内容,如果未能解决你的问题,请参考以下文章

在 UICollectionView 底部加载其他单元格

webform c#中的datagridview在页脚中显示查询

图标不会显示在台式计算机的页脚中

Laravel 4:如何在页脚中显示查询结果

想在反应原生应用程序的页脚中显示广告

初蒙数据表。计算金额并在页脚中显示