2017-07-21 1 views
4

私はUICollectionViewに表示されている項目に関して問題を抱えていますが、説明する方法がわかりませんが、誰かがこれを手伝うことができるように詳細に説明します。現在のセルが表示されていない場合、UICollectionViewは間違った項目数を表示しますか?

現在、にはUICollectionViewが含まれています。各UICollectionCellには画像があり、その画像はindexPathから取得されます。

次のサンプルコードはUICollectionViewを含むUITableViewCellの私の実装です:

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 
    } 
} 

私は、画像を選択UICollectionViewCellでそれを設定し、UITableViewCellでそれを表示するには、私のMainViewControllerUIImagePickerControllerを使用していますようなので:

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は次のようになります

5画像が追加され

enter image description here

は、UITableViewCellが動的にその高さ、したがってself.mainVCTableView.reloadRowsコードを変更し、行が再ロードされるたびに9日、13日、17日には、など。画像はUITableViewCellの高さを拡張するために追加されます。

enter image description here

9日の画像が追加されたとき、私は持っていますです問題 、細胞がindexPath.itemがとして報告さ最初ある、self.mainVCTableView.reloadRowsが最終的に私のCustomTableViewCellcellForItemAtを呼び出す、と呼ばれるようときに画面に表示されていない場合、

..など、2、1、0から始まります

enter image description hereenter image description here

:(左)と(右)画像を UICollectionViewに追加されたときに

次の2つのスクリーンショットであります

右からも分かるように、6番目、7番目、8番目の画像は消えてしまい、9番目の画像があるはずの空の行スペースがあります。

テーブルビューダウンスクロールに示す:

enter image description here

画像を添加したindexPath.itemのうち印刷には、以下を示す:

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 

しかし、I回10thイメージを追加すると、imagesCollectionViewが読み込まれると、すべてのイメージが再度表示されます。

enter image description here

プリントアウト10日画像はショーを追加された後:

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() 
} 
関連する問題