如何在iOS中的CollectionView的页脚中添加加载指示器

Mee*_*shi 6 objective-c uitableview ios uicollectionview swift

如何为下一页加载数据时,如何在集合视图的页脚中添加加载指标?

在此输入图像描述

我想在Footer of Collection视图中加载指标与上图相同.但我无法做到这一点.

那可能与否......?否则,我必须使用tableview创建这种类型的列表视图.因为我们可以轻松地将这些东西管理成tableview.

任何帮助,将不胜感激.

小智 6

最简单的解决方案是将活动指示器添加到视图并将该视图添加到 UICollectionReusableView。

在集合视图中:

private let footerView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.white)


  override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.register(CollectionViewFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "Footer")
        (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.footerReferenceSize = CGSize(width: collectionView.bounds.width, height: 50)
        
    }

   override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionFooter {
            let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath)
            footer.addSubview(footerView)
            footerView.frame = CGRect(x: 0, y: 0, width: collectionView.bounds.width, height: 50)
            return footer
        }
        return UICollectionReusableView()
    }
Run Code Online (Sandbox Code Playgroud)

定制类:如果需要,请在此处添加您的定制

public class CollectionViewFooterView: UICollectionReusableView {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

只需使用:footerView.startAnimating() & footerView.stopAnimating() 开始或停止动画

***** 希望你喜欢 。******


Pra*_*iya 5

在ViewController底部添加UIView的最简单方法是将ActivityIndi​​cator添加到该视图并将视图的属性隐藏为已选中,创建该视图的IBOutlet,同时在加载数据后从服务器集出口的属性加载数据hidden = NO再次隐藏视图.我在使用UICollectionViews或UITableView的一些应用程序中完成了同样的事情

编辑

另一种方法是在CollectionView或tableview的页脚中添加加载指示符,然后在下面的方法中进行网络调用

对于CollectionView:

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)

对于TableView:

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section;
Run Code Online (Sandbox Code Playgroud)

Swift 4.2

对于CollectionView:

func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)

对于TableView:

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int)
Run Code Online (Sandbox Code Playgroud)


Nit*_*esh 5

我在CCBottomRefreshControl 的帮助下找到了一个非常简单的解决方案 你需要把它当作简单的 UIRefreshController

let bottomRefreshController = UIRefreshControl()
bottomRefreshController.triggerVerticalOffset = 50
bottomRefreshController.addTarget(self, action: #selector(ViewController.refreshBottom), forControlEvents: .ValueChanged)

collectionView.bottomRefreshControl = bottomRefreshController

func refreshBottom() {
     //api call for loading more data
     loadMoreData() 
}
Run Code Online (Sandbox Code Playgroud)

以及您认为应该简单地停止装载机的地方

collectionView.bottomRefreshControl.endRefreshing()
Run Code Online (Sandbox Code Playgroud)