【发布时间】:2017-11-11 14:09:01
【问题描述】:
我的应用中有一个collectionView,其中只有1 个部分从API 下载数据。我有一个分页,我正在尝试在我的 collectionView 中添加一个加载页脚。标题正常显示。我在这个页脚单元格中有一个 UIActivityIndicatorView 和一个 UILabel。当触发第一个限制时,单元格中存在 2 个元素,但是当触发第二个限制时,UIActivityIndicatorView 不存在。
你对此有什么想法吗?
单元格的代码(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()
}
单元格注册良好。
触发第一个限制时会发生什么:
还有第二个:
【问题讨论】:
-
UIActivityIndicatorView 具有名为
hidesWhenStopped的属性。由于出队标头,如果您没有正确配置它,它会使用以前的状态。您可以覆盖prepareForReuse方法,您将在其中开始动画。 -
谢谢@MaksymMusiienko,我已经根据您的评论回答了我的帖子。你能确认这就是你的意思吗?
标签: ios swift uicollectionview uiactivityindicatorview uicollectionreusableview