2017-07-21 47 views
4

我有一個關於我的UICollectionView中顯示的項目的問題,我不知道如何解釋,但我會盡我所能詳細描述,希望有人能夠幫助解決這個問題。當前單元格不可見時,UICollectionView顯示錯誤的項目數量?

目前,我有一個UITableViewCell包含一個UICollectionView。每個UICollectionCell包含一個圖像,從它的indexPath檢索。

以下示例代碼是我的執行包含UITableViewCellUICollectionView

extension MainViewController: UITableViewDelegate 
{ 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     var cell: UITableViewCell = UITableViewCell() 

     .... 

     if indexPath.section == 4 
     { 
      if let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomTableViewCell 
      { 
       if images_ARRAY.isEmpty == false 
       { 
        customCell.images_ARRAY = images_ARRAY 
        customCell.awakeFromNib() 
       } 

       return imagesCell 
      } 
     } 

     return cell 
    } 
} 

class CustomTableViewCell: UITableViewCell 
{ 
    @IBOutlet var imagesCollectionView: UICollectionView! 

    var images_ARRAY = [UIImage]() 

    var images = [INSPhotoViewable]() 

    override func awakeFromNib() 
    { 
     super.awakeFromNib() 
     // Initialization code 

     print("awakeFromNib") 

     // Through through each image of the record 
     for image in images_ARRAY 
     { 
      images.append(INSPhoto(image: image, thumbnailImage: SomeClass.resized(originalImage: image, withPercentage: 0.3))) 
     } 

     imagesCollectionView.dataSource = self 
     imagesCollectionView.delegate = self 
    } 

    .... 

    override func prepareForReuse() 
    { 
     super.prepareForReuse() 

     print("prepareForReuse") 

     images.removeAll() 
    } 
} 

extension CustomTableViewCell: UICollectionViewDataSource 
{ 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
     return images.count 
    } 

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

     print("indexPath.row = \(indexPath.item)") 

     cell.populateWithPhoto(images[indexPath.item]) 

     return cell 
    } 
} 

我使用的是UIImagePickerController在我MainViewController選擇一個圖像,在UICollectionViewCell設置它,而在UITableViewCell顯示它像這樣:

extension MainViewController: UIImagePickerControllerDelegate 
{ 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
    { 
     if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage 
     { 
      // Store and display the image 
      if let cell = mainVCTableView.cellForRow(at: IndexPath(row: 0, section: 4)) as? CustomTableViewCell 
      { 
       cell.images_ARRAY.append(selectedImage) 

       // Remove any existing images before adding all images again 
       cell.images.removeAll() 

       cell.awakeFromNib() 

       cell.imagesCollectionView.reloadData() 

       images_ARRAY = cell.images_ARRAY 

       if ((images_ARRAY.count - 1) % 4) == 0 && 
         images_ARRAY.count != 1 
       { 
        self.mainVCTableView.reloadRows(at: [ IndexPath(row: 0, section: 4) ], with: UITableViewRowAnimation.none) 
       } 
      } 
     } 

     picker.dismiss(animated: true, completion: nil) 
    } 

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
    { 
     picker.dismiss(animated: true, completion: nil) 
    } 
} 

每次用戶選擇一個圖像,在images陣列的所有圖像被刪除,並且從重新添加在之前imagesCollectionView重新加載。當添加4個圖像,UITableViewCell看起來是這樣的:

enter image description here

在添加5圖像時,UITableViewCell動態改變其高度,因此self.mainVCTableView.reloadRows代碼,並將該行重載每當9日,13日,17日等。圖像被添加到擴大UITableViewCell的高度:

enter image description here

問題,我有添加的第9圖像時,是中,細胞是在屏幕上不可見的,所以當self.mainVCTableView.reloadRows被調用時,其最終調用我CustomTableViewCellcellForItemAt,所述indexPath.item最初報告爲,然後從0開始,1,2,等。

以下2米的屏幕截圖是當第八(左)和第九(右)圖像被添加到UICollectionView

enter image description hereenter image description here

正如您在右側看到的,第6,第7和第8張圖像已經消失,並且第9張圖像應該有空行空間。

向下滾動表視圖顯示:

enter image description here

打印出來的時候加入了9日圖像0​​的顯示以下內容:

indexPath.row = 4 
indexPath.row = 0 
indexPath.row = 1 
indexPath.row = 2 
indexPath.row = 3 
indexPath.row = 4 
indexPath.row = 5 
indexPath.row = 6 
indexPath.row = 7 

但是,一旦我添加第10張圖片,當imagesCollectionView重新加載時,所有圖片再次重新出現:

enter image description here

的圖像後,打印出已添加顯示:

indexPath.row = 0 
indexPath.row = 1 
indexPath.row = 2 
indexPath.row = 3 
indexPath.row = 4 
indexPath.row = 5 
indexPath.row = 6 
indexPath.row = 7 
indexPath.row = 8 
indexPath.row = 9 

我不太明白髮生了什麼?我爲這篇較長的文章道歉,但希望對我的問題具體說明,並展示插圖,以便有人可以有更好的理解來幫助我。

謝謝!

+0

如何確定單元格的高度? – vacawama

+0

添加9個單元格時,您是否可以滾動收集視圖? –

+0

@AhmadF,是的,我可以像上面描述的那樣滾動集合視圖,並在右圖中看到 – Pangu

回答

0

@Aravind A R對他的建議,但問題終於被重新加載內部cellForRowAtUICollectionView返回小區之前解決:

if images_ARRAY.isEmpty == false 
{ 
    customCell.images_ARRAY = images_ARRAY 
    customCell.awakeFromNib() 

    customCell.imagesCollectionView.reloadData() 
} 
相關問題