コレクションビューのセルをテーブルビューのセル内に実装していますが、コレクションビューのindexPathで奇妙なことが起こっています。UITableViewCellのSwift UICollectionView indexPathエラー
numberOfitemsInSectionが2の場合、numberOfItemsinSectionは3回、cellForItemAtは1回だけ発生します。コレクションビューには1つのセクションと2つのセルが必要です。ここ
は、ソースデータである:ここ
[["myDomain.jpg1", "myDomain.jpg2"]]
次のコードからのコンソール出力である:ここ
"number of items in section: 2"
"number of items in section: 2"
"number of items in section: 2"
"cell for item at [0, 0]"
"indexPath.section: 0"
"indexPath.row: 0"
"myDomain.jpg1"
は私のビューコントローラである:ここ
class WishListsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(WishListsCell.self, forCellReuseIdentifier: cellId)
self.view.addSubview(tableView)
}
func numberOfSections(in tableView: UITableView) -> Int {
return wishLists.count // 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WishListsCell
return cell
}
は私です表ビューセル:
class WishListsCell: UITableViewCell {
var collectionView: UICollectionView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: screenWidth, height: (screenWidth/4))
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.estimatedItemSize.height = (screenWidth/4)
layout.estimatedItemSize.width = screenWidth
collectionView = UICollectionView(frame: contentView.frame, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(WishListsCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
self.contentView.addSubview(collectionView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension WishListsCell: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let items = wishListUrls[section]
debugPrint("number of items in section: \(items.count)")
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! WishListsCollectionViewCell
debugPrint("cell for item at \(indexPath)")
debugPrint("indexPath.section: \(indexPath.section)")
debugPrint("indexPath.row: \(indexPath.row)")
if let imageUrl = wishListUrls[indexPath.section][indexPath.row] as String! {
debugPrint(imageUrl)
を使用します。https:// ashfurrowここにあなたがTableViewCell内CollectionViewに取得することができ落とし穴についてのブログ記事です。 collectionview内の/ – muescha
のコレクションビューでは、あなたは 'indexPath.row'の代わりに' indexPath.item'を使うべきです – muescha
それは違いはありませんでした。 'indexPath.row'と' indexPath.item'は同義語であり、入れ替えることができると私は理解しています – markhorrocks