Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色(2)
- UID
- 1066743
|
Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色(2)
4.设置选中颜色及圆角Cell。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell!.layer.cornerRadius = 4
cell?.backgroundColor = UIColor.yellowColor()
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell!.layer.cornerRadius = 4
cell?.backgroundColor = UIColor.whiteColor()
}
5.自定义圆角带边框的UICollectionViewCell。
//
// SHomeCell.swift
//
// Created by wangjie on 16/5/4.
// Copyright © 2016年 wangjie. All rights reserved.
//
import UIKit
class SHomeCell: UICollectionViewCell {
var titleLabel:UILabel?//title
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder has not been implemented")
}
func initView(){
titleLabel = UILabel(frame: CGRectMake(0, 0, (SCREEN_WIDTH - 20)/3, (SCREEN_WIDTH - 20)/3))
titleLabel?.layer.cornerRadius = 4
titleLabel?.layer.borderWidth = 0.5
titleLabel?.textAlignment = NSTextAlignment.Center
titleLabel?.layoutMargins = UIEdgeInsets(top:0, left:0, bottom:0, right:0)
self .addSubview(titleLabel!)
}
} |
|
|
|
|
|