TableView 페이징네이션(Pagingnation)

징니·2023년 11월 12일
0

Swift

목록 보기
7/9
var pageNum = 10
var totalCount = 0
var isTotalData = false
var isDataRunning = false

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height
    let height = scrollView.frame.height
        
    if offsetY > (contentHeight - height) && isDataRunning == false {
        isDataRunning = true
        pageNum += 10
        if pageNum > totalCount {
            pageNum = totalCount
        }
            
        if isTotalData == false {
            DispatchQueue.main.async {
                self.tvMain.reloadSections(IndexSet(integer: 1), with: .none)
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
                self.fetchList()
            })
        }
    }
}
     
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //마지막 인덱스 일때 data 추가 가능 여부를 변경한다.
    if indexPath.row == list.count - 1 {
        isDataRunning = false
    }
}
profile
iOS 개발자입니다

0개의 댓글