UICollectionView使用storyBoard完成设置自动布局,并附带简单选择功能
来源:互联网 发布:双十一淘宝营业额2017 编辑:程序博客网 时间:2024/06/05 03:03
UICollectionView使用storyBoard完成设置自动布局,并附带简单选择功能
一、storyBoard实现UICollectionView,并添加UICollectionViewCell。
UICollectionView:设定边界位置
TextLabelCell(UICollectionViewCell):
backView-实现选择背景框,边界与cell相同
contentLbl-内容显示,设置居中显示,不设定大小,不设定上下左右位置。
关联代理,实现代理方法。
二、UICollectionView设置自适应大小
实现代理方法中不需要指定cell大小,只需要三个代理 numberOfSections
numberOfItemsInSection
cellForItemAt indexPath
实现正常功能即可。
//设置为自适应方式 确定cell size,size不能为空,可以随意设置一个(collectionView.collectionViewLayout as! UICollectionViewFlowLayout).estimatedItemSize = CGSize(width: 20, height: 20)
三、修改UICollectionViewCell属性
在TextLabelCell重写preferredLayoutAttributesFitting
方法,这里可以自定义设置cell大小
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { let attributes = super.preferredLayoutAttributesFitting(layoutAttributes) attributes.frame = CGRect(x: 0, y: 0, width:NSString(string: contentLbl.text!).size(attributes: [NSFontAttributeName:contentLbl.font]).width+40, height: 40) //计算cellSize,当前要求高度固定40,宽度自适应,现在根据字符串求出所需宽度+42,contentLabel相对cell左右各有20的空间 //根据具体需求作灵活处理 return attributes}
如果只设置UICOllectionViewFlowLayout属性,不重写cell方法,在复用的时候会出现问题导致所求的size不对
四、最终结果
完成上面主要两步就能实现自适应大小功能,另外因为一个小需求,做一个多项选择的处理,下面给出完整代码:
ViewController.swift
class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { var selectedIndexArray:Array = Array<IndexPath>() var dataArray:Array = Array<String>() @IBOutlet var collectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //设置为自适应方式 确定cell size (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).estimatedItemSize = CGSize(width: 20, height: 20) //设置横向间距 (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).minimumInteritemSpacing = 5 //设置纵向间距-行间距 (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).minimumLineSpacing = 20 dataArray.append("第一个第二个第三个") for _ in 0 ..< 100 { let index = arc4random()%5; dataArray.append("\(index*index*index*500 + 8)") } } // MARK: UICollectionView DataSource func numberOfSections(in collectionView: UICollectionView) -> Int { return 1; } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataArray.count; } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TextLabelCell", for: indexPath) as! TextLabelCell cell.contentLbl.text = dataArray[indexPath.row] cell.cellStatusWithSelected(selected: ((selectedIndexArray.index(of: indexPath) != nil))) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let cell = collectionView.cellForItem(at: indexPath) as! TextLabelCell if let i = selectedIndexArray.index(of: indexPath) { print("selected:\(i)") selectedIndexArray.remove(at: i) cell.cellStatusWithSelected(selected: false) } else { selectedIndexArray.append(indexPath) cell.cellStatusWithSelected(selected: true) } } //保存 @IBAction func saveButtonClicked(_ sender: Any) { for index in selectedIndexArray { print(dataArray[index.row]) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
TextLabelCell.swift
class TextLabelCell: UICollectionViewCell { @IBOutlet var contentLbl: UILabel! @IBOutlet var backView: UIView! override func awakeFromNib() { backView.layer.borderWidth = 1; backView.layer.borderColor = UIColor.lightGray.cgColor; } func cellStatusWithSelected(selected:Bool) { if selected { backView.layer.borderColor = UIColor.red.cgColor; contentLbl.textColor = UIColor.red; } else { backView.layer.borderColor = UIColor.lightGray.cgColor; contentLbl.textColor = UIColor.lightGray; } } override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { let attributes = super.preferredLayoutAttributesFitting(layoutAttributes) attributes.frame = CGRect(x: 0, y: 0, width:NSString(string: contentLbl.text!).size(attributes: [NSFontAttributeName:contentLbl.font]).width+40, height: 40) //计算cellSize,当前要求高度固定40,宽度自适应,现在根据字符串求出所需宽度+42,contentLabel相对cell左右各有20的空间 //根据具体需求作灵活处理 return attributes }}
阅读全文
0 0
- UICollectionView使用storyBoard完成设置自动布局,并附带简单选择功能
- 使用UICollectionView和UITableView混合布局,完成简单天气demo
- IOS使用UICollectionView完成列表功能
- ios8 使用storyboard 进行自动布局(一)
- ios8 使用storyboard 进行自动布局(一)
- ios8 使用storyboard 进行自动布局(一)
- ios8 使用storyboard 进行自动布局
- ios8 使用storyboard 进行自动布局(一)
- UICollectionView的简单布局
- Storyboard自动布局
- storyBoard的自动布局
- storyboard的自动布局
- storyboard自动布局
- iOS8-storyboard自动布局
- storyboard 自动布局
- storyboard中的自动布局
- JSDT jQuery 自动完成功能设置
- iOS UICollectionView学习之一,UICollectionView + storyboard 简单应用
- Java的引用和实体
- 下拉框select chosen被遮盖
- 关于servlet的生命周期和线程的安全性
- 体验升级, 阿里云过保迁移不变公网IP
- 洛谷1064 金明的预算方案
- UICollectionView使用storyBoard完成设置自动布局,并附带简单选择功能
- Hadoop报错
- hdu 4747 Mex 递推计数
- linux学习笔记
- 使用JVisualVM远程监控Tomcat
- 居中元素的 scrollleft 的计算方法
- react native实战填坑
- 如何去掉Python列表中的单引号?
- hdu-6153