首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将自定义布局类连接到代码中的集合视图

将自定义布局类连接到代码中的集合视图
EN

Stack Overflow用户
提问于 2018-08-23 15:20:10
回答 1查看 615关注 0票数 1

我有一个关于自定义布局类的小问题,我需要将它连接到代码中的集合视图,而不是通过故事板,因为我是通过代码创建整个项目的。有什么帮助吗?

我的主要类

代码语言:javascript
运行
复制
import UIKit
import Firebase
class UserProfileController: UICollectionViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.backgroundColor = .white
        //fetchUser()
        collectionView?.dataSource = self
        collectionView?.delegate = self
        collectionView?.register(TestCell.self, forCellWithReuseIdentifier: mainCellId)    
    }


    fileprivate let mainCellId = "mainCellId"
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: mainCellId, for: indexPath) as! TestCell

        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15
    }
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
}

自定义布局类,它需要将其连接到代码而不是故事板中的集合视图。

代码语言:javascript
运行
复制
import UIKit

struct UltraVisualLayoutConstants {
    struct Cell {
        static let standardHeight: CGFloat = 100
        static let featuredHeight: CGFloat = 280
//        static let standardWidth: CGFloat = 100
//        static let featuredWidth: CGFloat = 280
    }
}

class UltraVisualLayout: UICollectionViewLayout {
    //  amount which users need to scroll before featured cell changes\
    let dragOffset: CGFloat = 180.0
    var cache = [UICollectionViewLayoutAttributes]()
    //return item index of current featured cell
    var featuredItemIndex: Int {
        get {
            //use max to ensure that featureditemindex never be < 0
            return max(0 , Int(collectionView!.contentOffset.y / dragOffset))
        }
    }

    // returns value between 0 and 1 to represent how close the next cell becomes the featured cell
    var nextItemPercentegeOffset: CGFloat {
        get {
            return (collectionView!.contentOffset.y / dragOffset) - CGFloat(featuredItemIndex)
        }
    }    
    // return the width of collection view
    var width: CGFloat {
        get {

            guard let width = collectionView?.bounds.width else {return 0}
            return width
        }
    }
    // return the height of collection view
    var height: CGFloat {
        get {
            guard let height = collectionView?.bounds.height else {return 0}
            return height
        }
    }
    //returns the number of items in the collection view
    var numberOfItems: Int {
        get {
            return collectionView!.numberOfItems(inSection: 0)
        }
    }

    // MARK: UICollectionViewLayout
    // return the size of all content in collection view
     override var collectionViewContentSize: CGSize {
   let contentHeight = (CGFloat(numberOfItems) * dragOffset) + (height - dragOffset)
    return CGSize(width: width, height: contentHeight)
}
    override func prepare() {
        cache.removeAll(keepingCapacity: false)

        let standardHeight = UltraVisualLayoutConstants.Cell.standardHeight
        let featuredHeight = UltraVisualLayoutConstants.Cell.featuredHeight
        var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        var y: CGFloat = 0
        for item in 0..<numberOfItems {
            let indexPath = NSIndexPath(item: item, section: 0)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath as IndexPath)
            attributes.zIndex = item
            var height = standardHeight
            if indexPath.item == featuredItemIndex {
                let yOffset = standardHeight * nextItemPercentegeOffset
                y = collectionView!.contentOffset.y - yOffset
                height = featuredHeight
            } else if indexPath.item == (featuredItemIndex + 1) && indexPath.item != numberOfItems {
                let maxY = y + standardHeight
                height = standardHeight + max((featuredHeight - standardHeight) * nextItemPercentegeOffset, 0)
                y = maxY - height
            }
         frame = CGRect(x: 0, y: y, width: width, height: height)
            attributes.frame = frame
            cache.append(attributes)
            y = frame.maxY
        }

    }

    // return all attributes in cache whose frame intersects with the rect passed to the method
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var layoutAttributes = [UICollectionViewLayoutAttributes]()
        for attributes in cache {
            if attributes.frame.intersects(rect) {
                layoutAttributes.append(attributes)
            }
        }
        return layoutAttributes
    }

    // return true so that layout is continuously invalidated as the user scrolls
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true 
    }
EN

Stack Overflow用户

回答已采纳

发布于 2018-08-23 15:49:43

如果没有programmatically初始化程序,则无法设置布局。来自文档

布局 用于组织项的布局对象。集合视图存储对指定对象的强引用。绝不能是零

因为您似乎在使用故事板,所以您必须将其设置在那里。

在代码中创建布局类之后,当您单击故事板上的集合视图时,它将显示出来。默认布局将设置为Flow,当您将其更改为Custom时,将显示一个新的类字段。单击该命令时,它将列出布局类。你可以从那里选择并设置它。

但是,如果要以编程方式初始化情节提要,则只需将其作为参数传递给初始化程序。

代码语言:javascript
运行
复制
var collectionView = UICollectionView(frame: yourFrame, collectionViewLayout: customCollectionViewLayout)    // pass your custom collection view instance
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51989156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档