UIActivityIndi​​catorView 在 CollectionView Footer 中消失

UIActivityIndicatorView disappears in CollectionView Footer

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

你有什么想法吗?

cell的代码(BaseCell只是一个class以避免每次都点击init和required 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)

    }

}

collection 委托方法:

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()
}

小区注册成功。

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

第二个:

根据@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()
    }
}