【问题标题】:UIActivityIndicatorView disappears in CollectionView FooterUIActivityIndi​​catorView 在 CollectionView 页脚中消失
【发布时间】:2017-11-11 14:09:01
【问题描述】:

我的应用中有一个collectionView,其中只有1 个部分从API 下载数据。我有一个分页,我正在尝试在我的 collectionView 中添加一个加载页脚。标题正常显示。我在这个页脚单元格中有一个 UIActivityIndi​​catorView 和一个 UILabel。当触发第一个限制时,单元格中存在 2 个元素,但是当触发第二个限制时,UIActivityIndi​​catorView 不存在。

你对此有什么想法吗?

单元格的代码(BaseCell只是一个类,避免每次点击init和需要init):

class loadingCell: BaseCell {

    override func setupViews() {
        super.setupViews()

        backgroundColor = .yellow

        let activity = UIActivityIndicatorView()
        activity.backgroundColor = .red
        activity.startAnimating()
        activity.frame = CGRect(x: 10, y: 20, width: 20, height: 20)
        let label = UILabel()
        label.text = "hello"
        label.frame = CGRect(x: 100, y: 20, width: 40, height: 20)
        label.backgroundColor = .green


        addSubview(activity)
        addSubview(label)

    }

}

集合委托方法:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
     return CGSize(width: SCREENW, height: 50)
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    if kind == UICollectionElementKindSectionFooter {
        let loadingFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: loadingCell, for: indexPath)
        return loadingFooterView
    }

    return UICollectionReusableView()
}

单元格注册良好。

触发第一个限制时会发生什么:

还有第二个:

【问题讨论】:

  • UIActivityIndi​​catorView 具有名为 hidesWhenStopped 的属性。由于出队标头,如果您没有正确配置它,它会使用以前的状态。您可以覆盖prepareForReuse 方法,您将在其中开始动画。
  • 谢谢@MaksymMusiienko,我已经根据您的评论回答了我的帖子。你能确认这就是你的意思吗?

标签: ios swift uicollectionview uiactivityindicatorview uicollectionreusableview


【解决方案1】:

根据@MaksymMusiienko 的评论,我尝试了这个来重置微调器的动画。

class loadingCell: BaseCell {

    let activitySpinner: UIActivityIndicatorView = {
       let spinner = UIActivityIndicatorView()
        spinner.backgroundColor = .red
        spinner.startAnimating()
        spinner.frame = CGRect(x: 10, y: 20, width: 20, height: 20)
        return spinner

    }()

    override func setupViews() {
        super.setupViews()

        backgroundColor = .yellow

        let label = UILabel()
        label.text = "hello"
        label.frame = CGRect(x: 100, y: 20, width: 40, height: 20)
        label.backgroundColor = .green


        addSubview(activitySpinner)
        addSubview(label)

    }

    override func prepareForReuse() {
        super.prepareForReuse()
        activitySpinner.startAnimating()
    }
}

【讨论】:

  • 是的。如果您仅在加载元素时显示此单元格:) 它对您有用吗?
  • 是的,就像一个魅力:)
猜你喜欢
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多