Я уже реализовал разбиение на страницы в 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()
}
}
Код TableView:
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
}