之前研究swift,用到了UICollectionView,在此拿出来和大家分享一下。。。
下面是主要代码
/**
创建collectionView
*/
func createTypeCollection() {
let layout = UICollectionViewFlowLayout.init()
self.typeCollection = UICollectionView.init(frame: CGRectMake(0, MAX_HEIGHT(self.toolBtn!), SCREEN_WIDTH, SCREEN_HEIGHT - MAX_HEIGHT(self.toolBtn!)), collectionViewLayout: layout)
self.typeCollection?.backgroundColor = UIColor.clearColor()
//注册CollectionViewCell
self.typeCollection?.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"cell")
//注册headerView
self.typeCollection?.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "cell")
self.typeCollection?.delegate = self;
self.typeCollection?.dataSource = self;
self.view.addSubview(self.typeCollection!)
}
/**
设置组别
- parameter collectionView:
- returns: 返回几个组别
*/
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return self.typeArray.count
}
/**
设置每个组别内的分类数量
- parameter collectionView:
- parameter section: 当前组别
- returns: 返回个数
*/
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let dic = self.typeArray[section] as! NSDictionary
let array = dic["items"] as! Array<String>
return array.count
}
/**
设置collectionView headerView的高度
- parameter collectionView:
- parameter collectionViewLayout:
- parameter section: 第几个
- returns: 返回大小
*/
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSize.init(width: SCREEN_WIDTH, height: 30)
}
/**
设置每一个itme的大小
- parameter collectionView:
- parameter collectionViewLayout:
- parameter indexPath:
- returns: 大小
*/
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
let dic = self.typeArray[indexPath.section] as! NSDictionary
let array = dic["items"] as! Array<String>
let size = getTextRectSize(array[indexPath.row], number: 15, width: 300)
return CGSize.init(width: size.width + 25, height: 30)
}
/**
设置上下左右间距
- parameter collectionView:
- parameter collectionViewLayout:
- parameter section:
- returns: 返回间距
*/
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
return UIEdgeInsets.init(top: 5, left: 5, bottom: 5, right: 5)
}
/**
设置headerView
- parameter collectionView:
- parameter kind:
- parameter indexPath: 第几个
- returns: 返回
*/
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
let dic = self.typeArray[indexPath.section] as! NSDictionary
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "cell", forIndexPath: indexPath)
for v in headerView.subviews {
v.removeFromSuperview()
}
let headerLabel = UILabel.init(frame: CGRectMake(10, 0, SCREEN_WIDTH, 30))
headerLabel.textColor = BG_COLOR(38, g: 82, b: 67, a: 1)
headerLabel.font = UIFont.init(name: MY_FONT, size: 17)
headerLabel.text = dic["title"] as? String
headerView.addSubview(headerLabel)
return headerView
}
/**
设置cell显示内容以及复用
- parameter collectionView:
- parameter indexPath:
- returns: 返回cell
*/
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
let dic = self.typeArray[indexPath.section] as? NSDictionary
let array = dic!["items"] as? Array<String>
let title = array![indexPath.row] as String
cell.refreshLabel(title)
if self.selectCellPath == indexPath {
cell.selectCellType()
}else{
cell.cancelSelectCellType()
}
return cell
}
/**
选择collectionView cell的方法
- parameter collectionView:
- parameter indexPath:
*/
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.selectCellPath = indexPath
self.typeCollection?.reloadData()
}
/**
可以获取上一个选择的cell
- parameter collectionView:
- parameter indexPath:
*/
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
}
下面是设置数据的代码(本地数据)
/**
创建数据数组
*/
func createTypeData() {
self.typeArray = [
[
"title":"文学",
"items":[
"小说","漫画","青春文学","随笔","现当代诗","戏剧","传记","古诗词","外国诗歌","艺术","摄影"
]
],
[
"title":"人文社科",
"items":[
"历史","文化","古籍","心理学","哲学/宗教","政治/军事","社会科学","法律"
]
],
[
"title":"生活",
"items":[
"休闲/爱好","孕产/胎教","烹饪/美食","时尚/美妆","旅游/地图","家庭/家居","亲子/家教","两性关系","育儿/早教","保健/养生","体育/运动","手工/DIY"
]
],
[
"title":"经管",
"items":[
"管理","投资","理财","经济"
]
],
[
"title":"科技",
"items":[
"科普读物","建筑","医学","计算机/网络","农业/林业","自然科学","工业技术"
]
],
[
"title":"网络流行",
"items":[
"玄幻/奇幻","武侠/仙侠","都市/职业","历史/军事","游戏/竞技","科幻/灵异","言情"
]
],
]
}