【问题标题】:UITableView paging not accurately works swiftUITableView 分页不能准确快速地工作
【发布时间】:2020-02-22 04:26:16
【问题描述】:

我已经在 UITableView 中实现了分页,但不知何故它不能完美地工作。如果我有时向下滚动,它会给我Index out of bound error,即使没有向后滚动。 其次,我无法向上滚动到最高记录。

  • 页码在服务中很重要。
  • 我在 tableView 中有部分。
var currentPage: Int = 0
var isLoadingList : Bool = false

服务:

func getListFromServer(_ pageNumber: Int){
    self.isLoadingList = false

        DataProvider.main.serviceGetData(SearchType: SearchType, PageNumber: pageNumber, callback: {success, result in

        do{
            if(success){
                let decoder = JSONDecoder()
                let response = try decoder.decode(ResponseData.self, from: result! as! Data)
                self.Appdata = response.appointmentList

                DispatchQueue.main.async {
                    self.tableView.reloadData()

                }
                return true
            }else{

                return false
            }
        }catch let error {

            print(error as Any)
            return false
        }
    })
    }

分页和滚动代码:

func loadMoreItemsForList(){
    currentPage += 1

        switch currentPage {
        case 1,2,3:
            getListFromServer(currentPage)
        default:
            print("no data")
        }
    }

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (((scrollView.contentOffset.y + scrollView.frame.size.height) > scrollView.contentSize.height ) && !isLoadingList){
            self.isLoadingList = true
            self.loadMoreItemsForList()
        }
    }

表格视图代码:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return Appdata?[section].date ?? ""
    }
//
    func numberOfSections(in tableView: UITableView) -> Int {
        return Appdata?.count ?? 0

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(80)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return Appdata?[section].appointmentList?.count ?? 0

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AppTableViewCell", for: indexPath) as! AppTableViewCell
        let dic = Appdata![indexPath.section].appointmentList![indexPath.row]

        cell.topLabel.text = dic.projectName
        cell.midLabel.text = dic.subTitle


        return cell 
    }

【问题讨论】:

    标签: ios swift uitableview pagination


    【解决方案1】:

    首先,您每次都在替换数据源

    self.Appdata = response.appointmentList
    

    应该是

      self.Appdata?.append(contentsOf: response.appointmentListz ?? [])
    

    其次当获取数据集isLoadingList为true时,避免在滚动时发出很多请求,并在得到响应后再次使其为false

    func getListFromServer(_ pageNumber: Int){
            DataProvider.main.serviceGetData(SearchType: SearchType, PageNumber: pageNumber, callback: {success, result in
                  self.isLoadingList = false
    
    

    最后不要忘记在网络关闭时使用 [weak self]

    func getListFromServer(_ pageNumber: Int){
            DataProvider.main.serviceGetData(SearchType: SearchType, PageNumber: pageNumber, callback: { [weak self] success, result in
            guard let self = self else { return }
                  self.isLoadingList = false
    
    

    【讨论】:

    • 谢谢你让我实现它并回复你。
    猜你喜欢
    • 1970-01-01
    • 2016-03-21
    • 2014-10-22
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多