UITableViewCell indexPath 错误中的 Swift UICollectionView

标签 swift uitableview swift3 uicollectionviewcell

我正在 TableView 单元格内实现 Collection View 单元格,但 Collection View indexPath 发生了奇怪的事情。

当 numberOfitemsInSection 为 2 时,numberOfItemsinSection 被触发 3 次,然后 cellForItemAt 仅被触发一次。我希望 Collection View 有一个部分和 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"

这是我的 View Controller :

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
}

这是我的表格 View 单元格:

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)

最佳答案

我在您的 WishListsCell 扩展中没有看到 numberOfSections(in collectionView) 的实现。

根据您发布的片段我不能确定,但​​它可能看起来像。

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return wishListUrls.count
}

关于UITableViewCell indexPath 错误中的 Swift UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41623885/

相关文章:

swift - 从 Core Data 取数据只返回一个值

swift - 将文件从临时 url 复制到文档目录总是会抛出错误。 NSURLSessionDownloadTask

ios - 可调整大小的 Collection View 单元格

ios - 如何在 Swift 4.2 xcode 10 中更新 cocoapods

iphone - UITableView pushViewController 在选择一行时

ios - 如何在 ios Swift 3 中从模型创建 JSON

swift - 尝试呈现 * 其 View 不在窗口层次结构中

ios - UITableView.reload 将旧单元格留在 TableView 中但隐藏了

ios - 扩展的 UITableViewCell 无法正常工作?

swift - 如何检查给定日期在 Swift 3 中的 x 天数内