标题:
Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色(2)
[打印本页]
作者:
look_w
时间:
2019-2-18 20:51
标题:
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!)
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0