2016-06-14 2 views
2

온라인 데이터베이스의 이미지를 표시하는 UICollectionView이 있습니다. 먼저 10 개의 이미지를로드 한 다음 사용자가 UICollectionView의 맨 아래로 스크롤하면 백그라운드에서 다른 10 개의 이미지를로드하고 뷰를 새로 고칩니다. 이제이, 그러나 과정이 완료되고 그 아래로 더 스크롤하지 않는 한, 그는 그 새로운 내용이 통지하지 않습니다 사용자에게 아무 표시가 완벽하게 정상적으로가없는 작품UICollectionView 하단의 UIRefreshControl (더 많은 데이터로드)

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 
    if (self.localFeedContent != nil && self.localFeedContent!.count > 0) { 
     if indexPath.item == (self.localFeedContent!.count-1) { 
     loadMoreData() 
    } 
    } 

} 

:이 다음과 같은 방법을 사용하여 달성 추가되었습니다. UIRefreshControl을 구현하고 싶습니다. UICollectionView의 맨 아래에 부착되어 화면 상단의 일반 UIRefreshControl과 똑같은 동작을 모방합니다. 이미 화면 상단에 하나를 구현했지만 하단에서도이를 수행하는 방법을 잘 모르겠습니다.

답변

10

나는 유사한 것을 썼다 - 나는 그저 아래에 UIActivityIndicator을 보여주는 또 다른 셀을 반환했다. 그래서 데이터 소스가 비슷한 것 (당신이 UIActivityIndicator와 함께 새로운 UICollectionViewCell를 반환 할 때)

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return array.count + 1 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if indexPath.item == array.count{ 
     // return the new UICollectionViewCell with an activity indicator 
    } 
    // return your regular UICollectionViewCell 
} 

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.item == array.count{ 
     // loadMoreData() 
    } 
} 

또는 대신 willDisplayCell에서 loadMoreData()를 호출을, 당신은 cellForItemAtIndexPath 방법으로 호출 할 수있다. 둘 중 하나가 작동 할 수 있습니다.

+0

왜 내가 collectionView에서 indexPath.row를 사용하고 있는지 확실하지 않습니다. 아마 indexPath.item을 의미할까요? – xaphod

+0

@xaphod 둘 다 작동하지만 UICollectionView와 더 관련이 있기 때문에'.item'을 사용하여 내 대답을 편집합니다 :) – Daven

1

하단에 UIRefreshControl을 구현할 필요가 없습니다. scrollView 메소드를 구현하여 스크롤이 화면 하단에 도달 할 때 일부 작업을 수행하면됩니다. 조건 내에서 일부 플래그를 사용하여보다 구체적인 조치를 취할 수 있습니다.

override func scrollViewDidScroll(scrollView: UIScrollView) { 
     let threshold = 100.0 as CGFloat! 
     let contentOffset = scrollView.contentOffset.y 
     let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height; 
     if (maximumOffset - contentOffset <= threshold) && (maximumOffset - contentOffset != -5.0) {   //Add ten more rows and reload the table content. 

      runMoreAPI() 
     } 
    } 
+0

요점은 API가 데이터를로드하고 있음을 나타내는 것입니다. –

+0

이 메소드를 구현하는 하단에서 새로 고침 컨트롤이 보이지 않습니다. @ kileros –

+0

이 메소드는 스크롤이 화면 하단에 도달하면 runMoreApi()를 실행합니다. 아이디어는 : 바닥에 도달하면 새로 고침 – kileros

관련 문제