【发布时间】: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