我需要增加每5个匹配的计数
-
如果total_count是
0,1,2,3,4,5
,那么pageNum = 1 -
如果total_count为
5,6,7,8,9,10
,则pageNum = 2…
如。如果total_count为50,则pageNum = 50/5 = 10
这里total_count
= 3,但当我每次刷新currentPageNumberVM
增加,但为什么?
代码:对于上述逻辑,我已经编写了像currentPageNumberVM += 1 + Int((self.paginationData?.result?.total_count ?? 0 - 1) / 5)
这样的代码。但这里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()
}
}
}
您正在增加currentPageNumberVM
的值,因为您正在使用"加法赋值运算符";+=
.
将其改为"赋值";操作符=
,应该可以正常工作。
self.currentPageNumberVM = 1 + Int((self.paginationData?.result?.total_count ?? 0 - 1) / 5)
文档