私は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
でそれを表示するには、私のMainViewController
でUIImagePickerController
を使用していますようなので:
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
は次のようになります
は、UITableViewCell
が動的にその高さ、したがってself.mainVCTableView.reloadRows
コードを変更し、行が再ロードされるたびに9日、13日、17日には、など。画像はUITableViewCell
の高さを拡張するために追加されます。
9日の画像が追加されたとき、私は持っていますです問題 、細胞がindexPath.item
がとして報告さ最初ある、self.mainVCTableView.reloadRows
が最終的に私のCustomTableViewCell
でcellForItemAt
を呼び出す、と呼ばれるようときに画面に表示されていない場合、
UICollectionView
に追加されたときに
次の2つのスクリーンショットであります
右からも分かるように、6番目、7番目、8番目の画像は消えてしまい、9番目の画像があるはずの空の行スペースがあります。
テーブルビューダウンスクロールに示す:
画像を添加した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
が読み込まれると、すべてのイメージが再度表示されます。
プリントアウト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
私はかなり何が起こっているか理解していませんか?私は長いポストをお詫びしますが、誰かが私を助けるためにより良い理解を持つことができるように、私の問題に特化してイラストを見せたいと思っていました。
ありがとうございました!
は、どのようにセルの高さが決定されますか? – vacawama
9セルを追加すると、コレクションビューをスクロールできますか? –
@AhmadF、はい私は右の画像に記載されているようにコレクションビューをスクロールすることができます – Pangu