2017-03-05 1 views
1

페이지 매김을 지원하는 API가 있습니다.collectionview에 더 많은로드를 수행하는 방법은 무엇입니까?

"pagination":{ 
    "total":355, 
    "totalPages":10, 
    "page":1, 
    "nextPage":2, 
    "nextPageUrl":"http://api..................?page=2" } 

내 목표는 nextPageUrl의 이미지를 내 collectionview에 추가하는 것입니다. 그래서 어떻게 할 수 있습니까?

조언이나 코드 샘플이 필요하십니까? 저는 신속하게 새로운 사람입니다. 고마워요 :)

답변

1

사용자가이 단추를 누를 때마다 단추를 다시 추가해야하므로 서버를 다시 호출하지만 다음 페이지를 호출합니다. 그런 다음받은 데이터를 이전 데이터에 추가하고 collectionView.reloadData를 호출합니다. 사용자가 단추를 사용하지 않고도 작업을 수행 할 수 있습니다. 사용자가 컬렉션보기의 끝에 도달하면 자동으로 시작됩니다. 컬렉션 뷰의 아래쪽에 사용자가 스크롤, 당신은 글로벌 스레드에서 더로드 요청을 트리거 할 때

if (CGRectGetMaxY(scrollView.bounds) == scrollView.contentSize.height) { 
    callToServer() 
} 
0

당신은, 그것은 자동으로 만들 수 있습니다

당신은 두 번째 그런 식으로 작업을 수행 할 수 있습니다 새 데이터로 컬렉션보기를 다시로드 할 준비가되었을 때보 다. 이 접근법은 물론 귀하의 요구에 맞게 조정해야합니다. 다음은 유사 코드 예제입니다 (질문 태그로 인해 신속하게).

class VideoList { 
    var dataModel: [SomeDataModelType] 
    var isLoadingMore = false 

    // .. variables and methods 

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
     let preloadingTreashold = Int(dataModel.count * 0.75) 
     let threasholdReached = indexPath.item >= preloadingTreashold 
     let reachedLastElement = indexPath.item == dataModel.count - 1 
     if !isLoadingMore { 
     loadMore() 
     } 
    } 

// Example function for load more, a little bit pseudocode. 
// The idea is only to illustrate the case. 
    func loadMore { 
    isLoadingMore = true 

    // distpatch loading on global queue, because we don't 
    // want to block the main thread 
    DispatchQueue.global().async { 
     // if your data model is a class You may need to copy it 
     // before You alter it, because otherwise, a user interaction 
     // can trigger an out of bounds exception or some other kind 
     // of nasty problem 
     var tmpDataModel = dataModel.copy() 

     // load new data model 
     var extendedDataModel = loadMore(to: tmpDataModel) 

     DispatchQueue.main.async { 
     // callback for loaid more completed or reloadData call 
     isLoadingMore = false   
     } 
    } 
    } 

    // other methods ... 
} 
관련 문제