ios - 使用自定义布局时未调用 sizeForItemAt 方法

标签 ios swift uicollectionview uikit

我正在为我的 Collection View 使用自定义布局,但如果我使用自定义布局,sizeForItemAt 方法不会调用。

我的自定义布局:

public class HorizontalCollectionViewLayout : UICollectionViewLayout {
    var itemSize = CGSize(width: 0, height: 0) {
        didSet {
            invalidateLayout()
        }
    }
    private var cellCount = 0
    private var boundsSize = CGSize(width: 0, height: 0)

    public override func prepare() {
        cellCount = self.collectionView!.numberOfItems(inSection: 0)
        boundsSize = self.collectionView!.bounds.size
    }
    public override var collectionViewContentSize: CGSize {
        let verticalItemsCount = Int(floor(boundsSize.height / itemSize.height))
        let horizontalItemsCount = Int(floor(boundsSize.width / itemSize.width))

        let itemsPerPage = verticalItemsCount * horizontalItemsCount
        let numberOfItems = cellCount
        let numberOfPages = Int(ceil(Double(numberOfItems) / Double(itemsPerPage)))

        var size = boundsSize
        size.width = CGFloat(numberOfPages) * boundsSize.width
        return size
    }

    public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var allAttributes = [UICollectionViewLayoutAttributes]()
        if cellCount > 0 {
        for i in 0...(cellCount-1) {
            let indexPath = IndexPath(row: i, section: 0)
            let attr = self.computeLayoutAttributesForCellAt(indexPath: indexPath)
            allAttributes.append(attr)
        }
        }
        return allAttributes
    }

    public override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return computeLayoutAttributesForCellAt(indexPath: indexPath)
    }

    public override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }

    private func computeLayoutAttributesForCellAt(indexPath:IndexPath)
        -> UICollectionViewLayoutAttributes {
            let row = indexPath.row
            let bounds = self.collectionView!.bounds

            let verticalItemsCount = Int(floor(boundsSize.height / itemSize.height))
            let horizontalItemsCount = Int(floor(boundsSize.width / itemSize.width))
            let itemsPerPage = verticalItemsCount * horizontalItemsCount

            let columnPosition = row % horizontalItemsCount
            let rowPosition = (row/horizontalItemsCount)%verticalItemsCount
            let itemPage = Int(floor(Double(row)/Double(itemsPerPage)))

            let attr = UICollectionViewLayoutAttributes(forCellWith: indexPath)

            var frame = CGRect(x: 0,y: 0,width: 0,height: 0)
            frame.origin.x = CGFloat(itemPage) * bounds.size.width + CGFloat(columnPosition) * itemSize.width
            frame.origin.y = CGFloat(rowPosition) * itemSize.height
            frame.size = itemSize
            attr.frame = frame

            return attr
    }
}

我在 viewDidLoad 方法中设置此布局:

let itemWidth = 100
let itemHeight = 100
let horizontalCV = HorizontalCollectionViewLayout();
horizontalCV.itemSize = CGSize(width: itemWidth, height: itemHeight)

instagramCollection.collectionViewLayout = horizontalCV
self.instagramCollection.delegate=self
self.instagramCollection.dataSource=self

其他 Collection View 方法工作正常,如 numberOfItemsInSectioncellForItemAt 等。但我无法为我的 Collection View 设置插图或单元格间距。

我试过下面的代码,下面的方法没有调用。

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize.init(width: (instagramCollection.frame.width / 3.0), height: instagramCollection.frame.height / 2.0)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10.0
}

我也尝试从 Storyboard设置插入和间距也没有用。

我的收藏 View 现在对所有插图和间距使用零。

如何解决这个问题?

最佳答案

它不会被调用,因为您使用的是自己的流布局,解决方法是按照以下步骤操作:

1-定义你自己的协议(protocol)

protocol MyCustomCollectionLayoutDelegate: AnyObject {
    func collectionView(_ collectionView: UICollectionView, sizeForItem indexPath: IndexPath) -> CGSize
}

2- 在您的自定义布局中添加委托(delegate)属性

final class HorizontalCollectionViewLayout: UICollectionViewFlowLayout {


     weak var delegate: MyCustomCollectionLayoutDelegate?
     ....
}

3- 将此协议(protocol)实现到您的 UICollectionViewController

class MyViewController: UICollectionViewController, MyCustomCollectionLayoutDelegate {

    var customLayout: HorizontalCollectionViewLayout? {
        let layout = collectionView?.collectionViewLayout as? DeviceDetailsCollectionLayout
        layout?.delegate = self
        return layout
    }

    func collectionView(_ collectionView: UICollectionView, sizeForItem indexPath: IndexPath) -> CGSize {
        return CGSize(width: 50, height: 60)
    }

}

4- 在自定义布局类中调用委托(delegate)方法

   public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

     let cellIndexPath = IndexPath(item: 0, section: 0)
     let customSize = delegate?.collectionView(collectionView, sizeForItem: cellIndexPath)

     // use your size ...

}

关于ios - 使用自定义布局时未调用 sizeForItemAt 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577850/

相关文章:

ios - UICollectionView 与 NINetworkImageViews 重复图像

ios - 导航栏和 UINavigationController 内容之间的差距

swift - 终端:Swift Build 命令不起作用

ios - 如何解决 No + [RCTConvert AIR MapCoordinate Array] 错误 : function found. 在 iOS 上 react native map

ios - 从 iOS 相机捕获视频

ios - 无法将类型 'String?.Type' 的值转换为预期参数类型 'String?'

ios - 通过委托(delegate)函数设置 Collection View 中进度条的值

ios - 滚动时 Swift UICollectionView 消失 label.text 和颜色混合可能是 BUG?

objective-c - 以编程方式将 UIWebView 添加到 UIImageView

ios - Swift UILabel 文本链接