Home > Net >  JSON value is same then why count changing in pagination for tableview in Swift
JSON value is same then why count changing in pagination for tableview in Swift

Time:06-28

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()
        }
    }
}

CodePudding user response:

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

  • Related