大佬教程收集整理的这篇文章主要介绍了ios – 使用自定义FlowLayout(Swift)时向CollectionView添加HeaderView,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
关于如何以编程方式添加headerView有一些答案,但它们在Objective-C中,如何使用Swift添加一个?
CollectionViewController { override func viewDidLoad() { super.viewDidLoad() // Setup Header self.collectionView!.registerClass(PinHeaderView.self,forSupplementaryViewOfKind:UICollectionElementKindSectionHeader,withReusEIDentifier: headerIdentifier) } override func collectionView(collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,aTindexPath indexPath: NSIndexPath) -> UICollectionReusableView { //1 switch kind { //2 case UICollectionElementKindSectionHeader: //3 let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind,withReusEIDentifier: "PinHeaderView",forIndexPath: indexPath) as! PinHeaderView headerView.pinHeaderLabel.text = boardName return headerView default: //4 fatalError("Unexpected element kind") } } } class PinHeaderView: UICollectionReusableView { @IBOutlet weak var pinHeaderLabel: UILabel! }
我的布局类:
import UIKit protocol PinterestLayoutDelegate { // 1. Method to ask the delegate for the height of the image func collectionView(collectionView:UICollectionView,heightForPhotoATindexPath indexPath:NSIndexPath,withWidth:CGFloat) -> CGFloat // 2. Method to ask the delegate for the height of the Annotation text func collectionView(collectionView: UICollectionView,heightForAnnotationATindexPath indexPath: NSIndexPath,withWidth width: CGFloat) -> CGFloat func columnsForDevice() -> Int } class PinterestLayoutAttributes:UICollectionViewLayoutAttributes { // 1. Custom attribute var photoHeight: CGFloat = 0.0 var headerHeight: CGFloat = 0.0 // 2. Override copyWithZone to conform to NSCopying protocol override func copyWithZone(zone: NSZonE) -> AnyObject { let copy = super.copyWithZone(zonE) as! PinterestLayoutAttributes copy.photoHeight = photoHeight return copy } // 3. Override isEqual override func isEqual(object: AnyObject?) -> Bool { if let attributtes = object as? PinterestLayoutAttributes { if( attributtes.photoHeight == photoHeight ) { return super.isEqual(object) } } return false } } class PinterestLayout: UICollectionViewLayout { //1. Pinterest Layout Delegate var delegate:PinterestLayoutDelegate! //2. Configurable properties //moved numberOfcolumns var celLPADding: CGFloat = 6.0 //3. Array to keep a cache of attributes. private var cache = [PinterestLayoutAttributes]() //4. Content height and size private var contentHeight:CGFloat = 0.0 private var contentWidth: CGFloat { let insets = collectionView!.contenTinset return CGRectGetWidth(collectionView!.bounds) - (insets.left + insets.right) } override class func layoutAttributesClass() -> AnyClass { return PinterestLayoutAttributes.self } override func prepareLayout() { // 1. Only calculate once //if cache.isEmpty { // 2. Pre-Calculates the X Offset for every column and adds an array to increment the currently max Y Offset for each column let numberOfcolumns = delegate.columnsForDevice() let columnWidth = contentWidth / CGFloat(numberOfcolumns) var xOffset = [CGFloat]() for column in 0 ..< numberOfcolumns { xOffset.append(CGFloat(column) * columnWidth ) } var column = 0 var yOffset = [CGFloat](count: numberOfcolumns,repeatedValue: 0) // 3. Iterates through the list of items in the first section for item in 0 ..< collectionView!.numberOfItemsInSection(0) { let indexPath = NSIndexPath(forItem: item,inSection: 0) // 4. Asks the delegate for the height of the picture and the Annotation and calculates the cell frame. let width = columnWidth - celLPADding*2 let photoHeight = delegate.collectionView(collectionView!,heightForPhotoATindexPath: indexPath,withWidth:width) let AnnotationHeight = delegate.collectionView(collectionView!,heightForAnnotationATindexPath: indexPath,withWidth: width) let height = celLPADding + photoHeight + AnnotationHeight + celLPADding let frame = CGRect(x: xOffset[column],y: yOffset[column],width: columnWidth,height: height) let insetFrame = CGRecTinset(frame,celLPADding,celLPADding) // 5. Creates an UICollectionViewLayoutItem with the frame and add it to the cache let attributes = PinterestLayoutAttributes(forCellWithIndexPath: indexPath) attributes.photoHeight = photoHeight attributes.frame = insetFrame cache.append(attributes) // 6. updates the collection view content height contentHeight = max(contentHeight,CGRectGetMaxY(framE)) yOffset[column] = yOffset[column] + height column = column >= (numberOfcolumns - 1) ? 0 : ++column } //} } override func layoutAttributesForSupplementaryViewOfKind(elementKind: String,aTindexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let attributes = PinterestLayoutAttributes(forSupplementaryViewOfKind: elementKind,withIndexPath: indexPath) attributes.headerHeight = 100.0 attributes.frame = (self.collectionView?.framE)! return attributes } override func collectionViewContentSize() -> CGSize { return CGSize(width: contentWidth,height: contentHeight) } override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var layoutAttributes = [UICollectionViewLayoutAttributes]() // Loop through the cache and look for items in the rect for attributes in cache { if CGRecTintersectsRect(attributes.frame,rect ) { layoutAttributes.append(attributes) } } return layoutAttributes } }
self.collectionView!.registerClass(PinHeaderView.self,forSupplementaryViewOfKind:UICollectionElementKindSectionHeader,withReusEIDentifier:headerIdentifier)
viewForSupplementaryElementOfKind委托方法不完整,不处理案例UICollectionElementKindSectionFooter,对页眉视图执行与标题视图相同的操作.如果您没有看到它,请将标题视图图层的borderWidth设置为大于0的值,它可能会显示出来.
以上是大佬教程为你收集整理的ios – 使用自定义FlowLayout(Swift)时向CollectionView添加HeaderView全部内容,希望文章能够帮你解决ios – 使用自定义FlowLayout(Swift)时向CollectionView添加HeaderView所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。