json值是相同的,那么为什么在swift中的tableview在分页上进行计数

发布于 2025-02-11 14:05:42 字数 2399 浏览 1 评论 0原文

如果total_count是0,1,2,3,4,5,则我需要增加每5个匹配的计数

  1. ,然后pagenum = 1

  2. 如果total_count是5,6,7,8,9,10然后Pagenum = 2 ...

对于例如,如果total_count为50,则pagenum = 50/5 = 10

这里

在 我已经编写了这样的代码currentPagenumbervm + = 1 + int((self.paginationdata?result?.total_count ?? 0-1) / 5) / 5)< / code>。但是这里total_count保持不变。但是,当我刷新currentpagenumbervm一直在增加。如何解决这个问题?

var currentPageNumberVM: Int = 1
var isLoadingList: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    self.currentPageNumberVM = 1
    self.PagenationMachesService(currentPageNumberVM)
}

private var paginationData = PaginationMaches(dictionary: NSDictionary())

func PagenationMachesService(_ pageNumber: Int) {

    let param = ["slug": slugVal ?? "", "page_no": currentPageNumberVM] as [String: Any]

    APIReqeustManager.sharedInstance.serviceCall(
        param: param as [String: Any], method: .post, url: CommonUrl.get_matches
    ) { [weak self] (resp) in

        if (self?.currentPageNumberVM)! > 1 {
            if (PaginationMaches(dictionary: resp.dict as NSDictionary? ?? NSDictionary())?.result?
                .matches ?? [Matches]()).count != 0
            {
                self?.paginationData?.result?.matches! +=
                    PaginationMaches(dictionary: resp.dict as NSDictionary? ?? NSDictionary())?
                    .result?.matches ?? [Matches]()
            } else {
                self?.showSingleButtonAlertWithoutAction(title: "NO more data to show")
            }
        } else {
            self?.paginationData = PaginationMaches(
                dictionary: resp.dict as NSDictionary? ?? NSDictionary())
        }

        self?.isLoadingList = false
        self?.machesTable.reloadData()
    }
}

@objc func refresh() {
    self.currentPageNumberVM += 1 + Int((self.paginationData?.result?.total_count ?? 0 - 1) / 5)
    self.PagenationMachesService(currentPageNumberVM)
}

extension MatchesVC {
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

        if (scrollView.contentOffset.y + scrollView.frame.size.height)
            >= scrollView.contentSize.height
        {
            self.isLoadingList = true
            self.refresh()
        }
    }
}

I need to increase count for every 5 matches

  1. if total_count is 0,1,2,3,4,5 then pageNum = 1

  2. if total_count is 5,6,7,8,9,10 then pageNum = 2...

for eg. if total_count is 50 then pageNum = 50/5 = 10

here total_count = 3 but when I refresh each time currentPageNumberVM is increasing, but why?

code: for above logic i have written code like this currentPageNumberVM += 1 + Int((self.paginationData?.result?.total_count ?? 0 - 1) / 5). But here total_count stays the same. But when I refresh currentPageNumberVM is increasing all the time. How to solve this issue?

var currentPageNumberVM: Int = 1
var isLoadingList: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    self.currentPageNumberVM = 1
    self.PagenationMachesService(currentPageNumberVM)
}

private var paginationData = PaginationMaches(dictionary: NSDictionary())

func PagenationMachesService(_ pageNumber: Int) {

    let param = ["slug": slugVal ?? "", "page_no": currentPageNumberVM] as [String: Any]

    APIReqeustManager.sharedInstance.serviceCall(
        param: param as [String: Any], method: .post, url: CommonUrl.get_matches
    ) { [weak self] (resp) in

        if (self?.currentPageNumberVM)! > 1 {
            if (PaginationMaches(dictionary: resp.dict as NSDictionary? ?? NSDictionary())?.result?
                .matches ?? [Matches]()).count != 0
            {
                self?.paginationData?.result?.matches! +=
                    PaginationMaches(dictionary: resp.dict as NSDictionary? ?? NSDictionary())?
                    .result?.matches ?? [Matches]()
            } else {
                self?.showSingleButtonAlertWithoutAction(title: "NO more data to show")
            }
        } else {
            self?.paginationData = PaginationMaches(
                dictionary: resp.dict as NSDictionary? ?? NSDictionary())
        }

        self?.isLoadingList = false
        self?.machesTable.reloadData()
    }
}

@objc func refresh() {
    self.currentPageNumberVM += 1 + Int((self.paginationData?.result?.total_count ?? 0 - 1) / 5)
    self.PagenationMachesService(currentPageNumberVM)
}

extension MatchesVC {
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

        if (scrollView.contentOffset.y + scrollView.frame.size.height)
            >= scrollView.contentSize.height
        {
            self.isLoadingList = true
            self.refresh()
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-02-18 14:05:42

您正在增加CurrentPageNumberVM的值,因为您正在使用“添加分配运算符” +=

将其更改为“分配”操作员=,它应该正常工作。

self.currentPageNumberVM = 1 + Int((self.paginationData?.result?.total_count ?? 0 - 1) / 5)

文档

You are increasing the value of currentPageNumberVM because you are using the "addition assignment operator" +=.

Change it to the "assignment" operator = and it should work fine.

self.currentPageNumberVM = 1 + Int((self.paginationData?.result?.total_count ?? 0 - 1) / 5)

documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文