标题:
Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色
[打印本页]
作者:
look_w
时间:
2019-2-18 20:50
标题:
Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色
1.设置Header布局SHomeHeader,继承自UICollectionReusableView。
//
// SHomeHeader.swift
//
// Created by wangjie on 16/5/4.
// Copyright © 2016年 wangjie. All rights reserved.
//
import UIKit
class SHomeHeader: UICollectionReusableView {
var titleLabel:UILabel?//title
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
func initView(){
titleLabel = UILabel(frame: CGRectMake(0, 0, SCREEN_WIDTH, 30))
titleLabel?.backgroundColor = HEADER_BG_COLOR
self .addSubview(titleLabel!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder
has not been implemented")
}
}
2.为UICollectionReusableView注册header。
//注册header
collectionView!.registerClass(SHomeHeader.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
3.自定义Header并设置宽高。
//设置HeadView的宽高
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSize(width: SCREEN_WIDTH, height: headerHeight)
}
//返回自定义HeadView
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
var v = SHomeHeader()
if kind == UICollectionElementKindSectionHeader{
v = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerIdentifier, forIndexPath: indexPath) as! SHomeHeader
let title:String = headerArr[indexPath.section] as! String
v.titleLabel?.text = title
}
return v
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0