ios - 不同维度的单个 CollectionView 中的两个单元格

标签 ios swift uikit

我在一个 Collection View 中有两个单元格。它们都有不同的维度,但由于某些原因在运行时两个单元格似乎具有相同的维度。

这是我的设置:

internal func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if cellType == .books {
        return books.count
    } else if cellType == .spotlights {
        return spotlights.count
    } else {
        return 0
    }
}

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if cellType == .books {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "booksCollectionCell", for: indexPath) as! BooksCollectionCell
        let bookTitle = books[indexPath.item].title
        let authors = books[indexPath.item].authors
        cell.configureCell(title: bookTitle, authorNames: authors)
        return cell
    } else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spotlightsCollectionCell", for: indexPath) as! spotlightsCollectionViewCell
        cell.configureCell(image: #imageLiteral(resourceName: "testImage"))
        return cell
    }
}

这是我的 Storyboard截图:

enter image description here

这是为 collectionView 设置的:

enter image description here

编辑:所以我通过将这段代码放在 cellForItem 中设法找到了某个地方(感谢 cpatmulloy):

cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: 180.0, height: 130.0)

但是,结果如下(查看 TableView 中的最后一个单元格):

enter image description here

最佳答案

尝试在 cellForItemAtIndex Path 函数中显式设置单元格的高度和宽度。

一定要自己设置这些示例属性(即 bookCellWidth)

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if cellType == .books {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "booksCollectionCell", for: indexPath) as! BooksCollectionCell
            let bookTitle = books[indexPath.item].title
            let authors = books[indexPath.item].authors
            cell.configureCell(title: bookTitle, authorNames: authors)

            cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: bookCellWidth, height: bookCellHeight)

            return cell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spotlightsCollectionCell", for: indexPath) as! spotlightsCollectionViewCell
            cell.configureCell(image: #imageLiteral(resourceName: "testImage"))

            cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y, width: spotlightCellWidth, height: spotlightCellHeight)

            return cell
        }
    }

关于ios - 不同维度的单个 CollectionView 中的两个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41108838/

相关文章:

ios - 使用委托(delegate)/协议(protocol)将数据传递给第三方 View Controller

iphone - UITextField 键盘不出现

iphone - 为什么 UIKit 中有这么多方法将 View 连接到事件?

ios - 如何更改 ios swift3 中的特定单元格颜色

ios - AVAssetTrack 的 Assets 属性有时为零

ios - 删除标签栏 Controller 中的后退按钮文本更多导航 Controller

ios - "Different language linkage"— xcode 4 中的警告,xcode 5 中的错误

swift - 为什么我的渐变层比我应用它的 UITextField 小?

ios - 将 UIButton 放置在具有约束的 UIScrollView 底部

ios - 在 UILabel 中将文本垂直对齐到顶部