【问题标题】:Why willDisplay is called multiple times in for collectionView in swift?为什么在swift中为collectionView多次调用willDisplay?
【发布时间】:2021-10-15 13:02:39
【问题描述】:

我正在使用带有分页的 collectionView,但 willDisplay 方法被多次调用,我不知道为什么。如果有人有任何想法,请帮助我...................................................... ......

func collectionView(_ collectionView: UICollectionView, willDisplay cell: func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard gridCollectionView!.contentOffset.y >= (gridCollectionView!.contentSize.height - gridCollectionView!.bounds.size.height),
          !isRefreshing else { return }
    isRefreshing = !isRefreshing
    offset += limit
    
    getAllExcrcise(keyword: "", limit: limit, offset: offset)
    
}

API调用函数

func getAllExcrcise(keyword: String = "", limit: Int = 10, offset: Int = 0, clearAll:Bool = false) {
    
    if clearAll {
        self.allExcercise.removeAll()
    }
    
    var params = [String: Any]()
    params = [
                "keyword": keyword,
                "limit": limit,
                "offset": offset,
                "muscle" : bodyPartSelectedArr,
                "equipment": categorySelectedArr
        
             ]
    
    WebServiceHelper.postWebServiceCall(Constants.baseURL + "getAllExcercises", params: params, isShowLoader: true, success: { (responceObj) in
        
        let statusMsg = GetAllExcerciseLevelOneData(json: responceObj)
        
        if statusMsg.status {
            let respArr = responceObj["data"].arrayValue
            for item in respArr {
                let dataModel = GetAllExcercise(json: item)
                
                for selectedVal in self.selectedItem {
                    if selectedVal.id == dataModel.id {
                        dataModel.isCellSelected = true
                    }
                }
                
                self.allExcercise.append(dataModel)
            }
            self.totalPages = statusMsg.totalPages
        } else {
            //CommonUtils.showToastMessage(message: statusMsg.message ?? "")
        }
        
        self.tableView?.reloadData()
        self.gridCollectionView?.reloadData()
    }
   , failure: { (failure) in
        print(failure)
   })
}

【问题讨论】:

  • 在表格/集合视图中每个单元格加载之前将显示方法调用
  • 那么我将如何实现分页
  • 你可以使用 UIScrollViewDelegate。
  • 你能用代码解释一下吗
  • 你想实现哪个滚动方向的分页?

标签: ios swift uicollectionview uikit


【解决方案1】:

您可以检查将在 willDisplay 方法中显示的单元格的当前索引,以及它的最后一个数组索引是否比您必须调用 getNextPageData。在获取数据时,您可以显示加载指示器,该指示器显示当前正在进行的数据加载。

【讨论】:

    【解决方案2】:

    willDisplay 方法在每次显示单元格之前调用。更好的方法是使用 scrollViewDidScroll,因为您的 collectionView 实际上是一个 scrollView。 我建议:

    private var currentPage: Int = 0
    private var isRefreshing: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        api(loadPage: 0)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard yourCollectionView.contentOffset.y >= (yourCollectionView.contentSize.height - yourCollectionView.bounds.size.height),
              !isRefreshing else { return }
        isRefreshing.toggle()
        page += 1
        api(loadPage: page)
    }
    

    在您的 api 调用是异步操作之后,您需要重新加载 collectionView 数据并在完成时切换 isRefreshing

    【讨论】:

    • 我们为什么使用 isRefreshing var
    • 但它只调用一次
    • 集合刷新时(api请求更多项目时)scrollViewDidScroll中的代码不能执行,因为页面计数器不冲突。 api加载项目后,切换isRefreshing,您可以在下次向下滚动时加载下一页。
    • 听不懂你想说什么,我的问题还没有解决:(
    【解决方案3】:

    willDisplay cell:UICollectionView 中的每个单元格显示并对用户可见之前被调用。如果单元格超出显示的视图(假设用户滑动到另一个单元格),系统会调用didEndDisplaying didEndDisplayingCell: 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多