我已经导入了一个可可豆荚,即PaginatedTableView,它内置了UIRefreshControl和分页功能。当我拉出页面时,应用程序由于索引超出范围错误而崩溃,我认为这是由于Refreshcontrol而发生的。
我试图找出错误的出处,但这似乎没问题。
lazy var refreshControltableView: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(self.handleRefreshtableView(_:)), for: UIControl.Event.valueChanged)
return refreshControl
}()
extension PaginatedTableView {
public func loadData(refresh: Bool = false) {
load(refresh: refresh)
}
@objc fileprivate func handleRefreshtableView(_ refreshControl: UIRefreshControl) {
load(refresh: true)
}
// All loading logic goes here i.e. showing/hiding of loaders and pagination
private func load(refresh: Bool = false) {
// reset page number if refresh
if refresh {
currentPage = firstPage
hasMoreData = true
}
// return if already loading or dont have any more data
if !hasMoreData || isLoading {
return }
// start loading
isLoading = true
paginatedDelegate?.loadMore(currentPage, pageSize, onSuccess: { hasMore in
self.hasMoreData = hasMore
self.currentPage += 1
self.isLoading = false
self.refreshControltableView.endRefreshing()
if self.customReloadDataBlock != nil {
self.customReloadDataBlock?()
} else {
self.reloadData()
}
}, onError: { _ in
self.refreshControltableView.endRefreshing()
self.isLoading = false
})
}
// Scroll to end detector
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
let height = scrollView.frame.size.height
let contentYoffset = scrollView.contentOffset.y
let distanceFromBottom = scrollView.contentSize.height - contentYoffset
if distanceFromBottom < height {
load()
}
}
}
func loadMore(_ pageNumber: Int, _ pageSize: Int, onSuccess: ((Bool) -> Void)?, onError: ((Error) -> Void)?) {
// Call your api here
if pageNumber < pages - 1 {
onSuccess?(true)
print("success")
}
else {
onSuccess?(false)
}
// Send true in onSuccess in case new data exists, sending false will disable pagination
// If page number is first, reset the list
if pageNumber == 0 {
cleaner()
}
myApi(pageNumber)
// else append the data to list
// If Api responds with error
// Else end success with flag true if more data available
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! myTableCell
cell.myLabel.text = self.labelArray[indexPath.row]
cell.myImage.image = self.imageArray[indexPath.row]
cell.myDateLabel.text = self.labelDateArray[indexPath.row]
return cell
}
loadMore函数在委托类中。 myApi函数查找数据并将其附加到列表中。 这是我认为与我的问题有关的Pod代码。预先感谢。