【问题标题】:How can I manage two different cells in numberofitemsinsection如何在 numberofitemsinsection 中管理两个不同的单元格
【发布时间】:2020-03-31 22:50:52
【问题描述】:

我想创建一个包含两个不同单元格的 collectionView。第一个单元格应显示一次,第二个单元格应尽可能频繁地显示,因为数组很大。结果应该类似于附加链接中的图像。这也是一个示例代码,可以更好地理解我的问题。感谢所有帮助我的人!!! ????

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


    switch indexpath { // No indexpath in numberofitemsinsection!
    case 0:
        return 1 //display one time the first cell
    default:
        return images.count // display as often as the array is large the second cell
    }

}

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    switch indexPath.row {
    case 0:
        let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "addImageCell", for: indexPath)
        return cell
    default:
        let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell

           cell.imageView.image = images[indexPath.row] 

        cell.delegate = self
        cell.selectedAtIndex = indexPath

        return cell
    }
}

Here is the collectionView I want to create

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell


    【解决方案1】:

    您可以在cellForItemAt 中实现此功能,您需要更改numberOfItemsInSection,如下所示:

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // return 1 more than our data array (the extra one will be the "add item" cell)
        return dataSourceArray.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        // if indexPath.item is less than data count, return a "Content" cell
        if indexPath.item < dataSourceArray.count {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
    
            // configure your ContentCell: cell. <attribute>
    
            return cell
        }
    
        // past the end of the data count, so return an "Add Item" cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell
    
        // configure your AddCell: cell. <attribute>
    
        return cell
    
    }
    

    为此,您需要创建一个ContentCellAddItemCell,还需要一个dataSourceArray 来存储您需要的所有数据。

    【讨论】:

    • 请添加最小正确答案。我相信回调闭包与回答问题无关。
    • 感谢您的回答,但这并没有解决我的问题。
    • 你确定?我编辑了我的答案,所以它更清楚一点。在您所说的其中一个 cmets 中,您有一个 indexOutOfBounce 。你不会得到这个解决方案。我的答案总是在最后一个index 处显示AddCell。如果您希望它始终是第一个,只需将 indexPath.item &lt; dataSourceArray.count 更改为 indexPath.item == 0
    【解决方案2】:

    你为什么不用这个?

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1 + theArray.count
    }
    

    【讨论】:

    • 您还需要调整indexPath.row 的值以访问cellForRowimages 的正确元素
    • 我有同样的想法将 array.count 加一,但我总是在 cellforrow 中得到一个错误。如何调整 cellForRow 中的 indexpath.row 值。我总是遇到致命错误:索引超出范围。
    • 只需减去您不想要的偏移量。 'indexPath.row - offset' 就可以了。
    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多