2016-12-14 26 views
0

我不知道這是否可能,但是,我試圖返回一個使用CollectionView cellForItem的單元格,只有在滿足某個條件時纔可以。UICollectionViewCell只顯示何時滿足某個標準

這裏是我的功能:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FriendsRecentCell", for: indexPath) as! FriendsRecentCell 

     var fObj = PFObject(className: FRIENDS_CLASS_NAME) 
     fObj = recentArray[indexPath.row] 

     // Get User Pointer 
     let userPointer = fObj[FRIENDS_IS_FRIEND_WITH] as! PFUser 
     userPointer.fetchIfNeededInBackground(block: { (user, error) in 
      if error == nil { 

       // Get heys sent to you as currentUser 
       let query = PFQuery(className: HEYS_CLASS_NAME) 
       query.whereKey(HEYS_RECEIVER, equalTo: PFUser.current()!) 
       query.whereKey(HEYS_SENDER, equalTo: userPointer) 
       query.countObjectsInBackground { (count, error) in 
        if count != 0 { 
         // Get User fullname 
         cell.userLabelRecent.text = "\(userPointer[USER_FULLNAME]!)" 

         // Get Avatar 
         cell.avatarImageRecent.image = UIImage(named: "avatar") 
         cell.avatarImageRecent.layer.cornerRadius = cell.avatarImageRecent.bounds.size.width/2 
         let imageFile = userPointer[USER_AVATAR] as? PFFile 
         imageFile?.getDataInBackground(block: { (imageData, error) in 
          if error == nil { 
           if let imageData = imageData { 
            cell.avatarImageRecent.image = UIImage(data:imageData) 
           }}}) 
        } else { 
        }} 

      }}) 

     return cell 

    } 

我只希望小區返回if count != 0。有沒有什麼辦法可以做到這一點?我完全失去了。

+0

我建議你預處理所有這些數據,把它放在一個數組中,然後加載你的collectionView。如果您有興趣,可以明天提供更多的指導。 – toddg

+0

是的我明天會喜歡一些幫助@toddg –

回答

0

在調用此方法的位置,您已告知集合視圖,該索引路徑中存在單元。現在你已經這樣做了,你提供一個單元;沒有辦法不 - 方法簽名是非可選的。

如果您只想在某些情況下出現某個單元格,那麼您必須在此過程中儘早處理它。當調用collectionView(_:numberOfItemsInSection:)時,您需要確定是否顯示單元格並返回正確數量的項目。然後你只會被要求提供這些細胞。

相關問題