现在,我使用此问题底部的代码进行分页,并且一切正常,但是我希望在分页开始后将一个加载单元添加到collectionView中,并在分页停止后将其删除。
我尝试了以下操作,但是它只是将加载单元添加到简历的底部,并且加载单元永远不会离开。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return datasource.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == tableData.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: loadingCell, for: indexPath) as! LoadingCell
cell.spinner.startAnimating()
return cell
}
return cell // this is a normal cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == tableData.count {
return CGSize(width: collectionView.frame.width, height: 40) // loading cell is 40 pts
}
return CGSize(width: width, height: 80) // normal cell is 80 pts
}
我也尝试使用节,但是发生了相同的问题
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return datasource.count
} else {
return 1
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: loadingCell, for: indexPath) as! LoadingCell
cell.spinner.startAnimating()
return cell
}
return cell // this is a normal cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.section == 1 {
return CGSize(width: collectionView.frame.width, height: 40) // loading cell is 40 pts
}
return CGSize(width: width, height: 80) // normal cell is 80 pts
}
仅当handlePaginating()被调用/完成时,如何才能删除添加和删除称重传感器?
var startKey: String?
override func viewDidLoad() {
super.viewDidLoad()
handlePagination()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
let contentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
if maximumOffset - contentOffset <= 10 {
handlePagination()
}
}
func handlePagination() {
if startKey == nil {
Database...Ref?.queryOrderedByKey().queryLimited(toLast: 10).observeSingleEvent(of: .value, with: { [weak self](snapshot) in
guard let children = snapshot.children.allObjects.first as? DataSnapshot else { return}
for child in snapshot.children.allObjects as! [DataSnapshot] {
// append child to datasource
}
self?.startKey = children.key
})
} else {
Database...Ref?.queryOrderedByKey().queryEnding(atValue: startKey!).queryLimited(toLast: 11).observeSingleEvent(of: .value, with: { [weak self](snapshot) in
guard let children = snapshot.children.allObjects.first as? DataSnapshot else { return}
for child in snapshot.children.allObjects as! [DataSnapshot] {
// insert child in datasource at statIndex
}
self?.startKey = children.key
})
}
}
答案 0 :(得分:0)
您必须在当前代码中添加一个额外的flag变量才能正常工作。当前代码中发生的事情是,当最后一个要显示的单元格将调用handlePagination
时,您将添加加载单元格而没有任何条件handlePagination
当前正在处理中。因此,在添加加载单元后,willDisplay
方法将再次调用api。
希望获得帮助。
E.G:
var isApiCalling = false
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let lastElement = tableData.count - 1
if indexPath.item == lastElement && !isApiCalling {
isApiCalling = true
handlePagination()
}
}
func handlePagination() {
//After you get response from API called. reset isApiCalling to false.
isApiCalling = false.
}
答案 1 :(得分:0)
我使用了@PratikPrajapati答案中的flag属性的概念以及我的问题的第一部分。
我声明了一个属性,最初将其设置为var isPaging = false
。然后在cellForItem
和sizeForItem
内部,检查bool值是true
还是false
。
在else
的{{1}}子句中,将标志设置为handlePagination()
,然后调用isPaging = true
,以便在重新加载简历时,collectionView.reloadData()
和cellForItem
现在将在加载单元内显示微调框,并将其高度设置为40。
在sizeForItem
子句的回调内部,我将标志设置回else
并再次调用false
,以便所有单元格都被更新,旋转器不再旋转,并且加载单元格的大小为collectionView.reloadData()
:
.zero
这是完整的代码:
Database...Ref?.queryOrderedByKey() ....
// callback
self?.isPaging = false
// ...
UIView.performWithoutAnimation {
self?.collectionView.reloadData()
}
...