2016-09-01 4 views
0

사용자 지정 셀에 UICollectionView을 구현하려고합니다. 셋업은 다음 입니다 :CollecitonView가 항상 데이터를 올바르게 표시하지 않습니다.

  • 4 세포
  • 내가 다운로드 한 이미지 =>의 데이터가 이미지 셀의 이미지 뷰를 채울 수 있다면.
  • else : 자리 표시자를 사용하십시오.

PFFilesimageFileDic:[String:PFFile] 내에 저장됩니다.

이것은 내 cellForItemAtIndexPath 업데이트 :

let collectionCell:SettingsCollectionViewCell = 
    collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", 
    forIndexPath: indexPath) as! SettingsCollectionViewCell 


    if indexPath.row < imageFileDic.count { 


     if let imageId = imageFileIds[indexPath.row] as? String { 

      if let imageData = imageFileDic[imageId]{ 

       collectionCell.collectionViewImage.file = imageData 
       collectionCell.collectionViewImage.loadInBackground() 

      } 

     } 
    } else{ 

     collectionCell.collectionViewButton.setImage(UIImage(), forState: .Normal) 
     collectionCell.collectionViewImage.image = UIImage(named: "CameraBild.png") 

    } 

지금 가끔 (1/5 회) 내 응용 프로그램을 두 번 이미지를 표시하거나 세포 NR의 위치로 결정한다. 4.

내 쿼리에 새 데이터를 추가하기 전에 항상 사전과 배열을 삭제하려고합니다.

아이디어가 있으십니까?

편집 : 이 내가 전화하고있는 PFQuery입니다 :

let imageQuery = PFQuery(className: "Images") 
    imageQuery.whereKey("sender", equalTo: objectID!) 
    imageQuery.addAscendingOrder("createdAt") 
    imageQuery.cachePolicy = .NetworkElseCache 

    imageQuery.findObjectsInBackgroundWithBlock { (objects, error) in 
     if error != nil { 
     createAlert(error!) 
     } 
     else{ 
      if let objects = objects{ 


       self.imageFileDic.removeAll() 
       self.imageFileIds.removeAll() 

       for object in objects{ 

        if let id = object.objectId{ 
         print("id found") 
     if let imageFile = object.objectForKey("imageFile") as? PFFile{ 

         self.imageFileIds.append(id) 
         self.imageFileDic[id] = imageFile 



          if object == objects.last{ 
          print(self.imageFileIds.count) 
          print(self.imageFileDic.count) 

          self.mainCollectionView.reloadData() 
          } 

         } 
        } 

       } 
      } 
     } 
    } 

} 
+0

에 이미지를 업데이트한다고 생각합니다. 매우 초기에 collectionCell.collectionViewImage.image = nill을 수행하지 않을 것입니다. –

+0

전체 cellForItemAtIndexPath를 게시하는 것 외에도 쿼리 코드를 게시하고 비동기 응답을 처리하는 방법을 설명하십시오. –

+0

재사용 가능한 셀은 셀 항목의 초기 값을 설정하지 않는다고 가정 할 수 있습니다. –

답변

1

나는 완전히 개봉 cellForItemAtIndexPath 방법을 추가 메인 쓰레드

dispatch_async(dispatch_get_main_queue()) { 
     collectionCell.collectionViewImage.file = imageData 
     collectionCell.collectionViewImage.loadInBackground() 
    } 
+0

정확하게 이것이 필요합니까? 내 cellForItem에서? – JVS

+0

예, cellForItemAtIndexPath – Giang

+0

내부에서 이것이 작동 할 것이라고 생각했지만 지금은 문제가 다시 발생했습니다. – JVS

관련 문제