Sye*_*riq 5 uicollectionview swift3
我有一个简单的集合视图测试(基于在线教程),它可以很好地独立运行。但是当我将它嵌入导航控制器时,它停止工作。我通过 (1) 创建一个 headerView(64 像素高)并将其添加到顶部的视图中来构建屏幕。(2) 我构建了一个集合视图并将其添加到 headerView。
这是代码:
import UIKit
class ViewController: UIViewController,
UICollectionViewDelegate, UICollectionViewDataSource,
UINavigationControllerDelegate
{
var collectionView : UICollectionView!
var topView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var frame = CGRect(x:0,y:128, width:view.frame.width, height:64)
topView = UIView(frame:frame)
self.view.addSubview(topView)
// CollectionView
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
layout.itemSize = CGSize(width: 50, height: 50)
frame = CGRect(x: 0, y: 0, width: Int(self.topView.frame.width), height: Int(self.topView.frame.height))
collectionView = UICollectionView (frame: frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.backgroundColor = UIColor.green
self.topView.addSubview(collectionView)
}
//MARK: - CollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath)
for v in cell.subviews {
v.removeFromSuperview()
}
cell.backgroundColor = UIColor.orange
let label = UILabel(frame: CGRect(x:0 , y:0 , width:50 , height:50))
label.text = "\(indexPath.item)"
label.textAlignment = .center
label.textColor = UIColor.white
cell.addSubview(label)
return cell
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Run Code Online (Sandbox Code Playgroud)
}
我无法按照所述使考沙尔的建议发挥作用。它确实给了我一个线索,由于我不明白的原因,我嵌入集合视图的视图的位置在 viewDidLoad 中被放错了位置。然而,通过将集合视图配置放在 viewDidAppear (而不是 viewDidLoad )中,效果很好。我将 y 位置偏移了 64 以清除导航栏,并将行高减小到 64。我还放置了代码以仅执行一次代码,以便从页面导航不会添加彼此顶部的多个视图。顺便说一句,我最初的目标是拥有水平滚动的单元格。在我的程序中,我有带有相应部分的表格视图,其想法是使用具有水平滚动单元格的行来移动到相应的部分。
\n\n代码如下所示:
\n\n //\n// CustomViewController.swift\n// DSM Tracker\n//\n// Created by Syed Tariq on 1/7/17.\n// Copyright \xc2\xa9 2017 com.syedtariq. All rights reserved.\n//\n\nimport UIKit\n\n\nclass ViewController: UIViewController,\n UICollectionViewDelegate,\n UICollectionViewDataSource,\n UINavigationControllerDelegate\n\n{\n\n var executeOnce = true\n var cellDimensions = [String:Int]()\n var cellHeight = 50\n var cellWidth = 120\n\n var collectionContainerView: UICollectionView!\n var navBar: UINavigationBar = UINavigationBar()\n\n\n // view constants\n var viewY = CGFloat()\n var viewX = CGFloat()\n var viewWidth = CGFloat()\n var viewHeight = CGFloat()\n\n\n // gaps from view edge\n let leftGap = CGFloat(20)\n let rightGap = CGFloat(20)\n\n\n // navbar constants\n let navBarHeight = CGFloat(64)\n\n var headerLabels = ["Cell 01","Cell 02","Cell 03","Cell 04","Cell 05","Cell 06","Cell 07","Cell 08","Cell 09","Cell 10","Cell 11","Cell 12","Cell 13","Cell 14","Cell 15","Cell 16"]\n\n\n override func viewDidLoad() {\n super.viewDidLoad()\n navBar.backgroundColor = UIColor.green\n executeOnce = true\n viewY = view.frame.origin.y\n viewX = view.frame.origin.x\n viewWidth = view.frame.width\n viewHeight = view.frame.height\n }\n\n\n\n func configureCollectionView () {\n if executeOnce {\n executeOnce = false\n let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()\n layout.scrollDirection = .horizontal\n layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)\n layout.itemSize = CGSize(width: cellWidth, height: cellHeight)\n\n let colWidth = viewWidth - leftGap - rightGap\n let colX = viewX + leftGap\n let colY = viewY + navBarHeight\n let colHeight = CGFloat(64)\n let frame = CGRect(x:colX, y:colY, width: colWidth, height: colHeight)\n //let frame = CGRect.zero\n collectionContainerView = UICollectionView (frame: frame, collectionViewLayout: layout)\n collectionContainerView.dataSource = self\n collectionContainerView.delegate = self\n collectionContainerView.autoresizingMask = [.flexibleLeftMargin,.flexibleLeftMargin,.flexibleBottomMargin,.flexibleRightMargin, .flexibleHeight, .flexibleWidth]\n collectionContainerView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")\n collectionContainerView.backgroundColor = UIColor.blue\n collectionContainerView.allowsSelection = true\n collectionContainerView.isScrollEnabled = true\n collectionContainerView.setNeedsDisplay()\n print("collectionContainerView.frame \\(collectionContainerView.frame)")\n view.addSubview(collectionContainerView)\n }\n }\n\n override func viewDidAppear(_ animated: Bool) {\n configureCollectionView()\n }\n\n func numberOfSections(in collectionView: UICollectionView) -> Int {\n return 1\n }\n\n func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n print("headerLabels.count \\(headerLabels.count)")\n return headerLabels.count\n }\n\n\n\n func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath)\n\n for v in cell.subviews {\n v.removeFromSuperview()\n }\n\n let cellTitle = headerLabels[indexPath.row]\n let cellTitleLines = cellTitle.components(separatedBy: " ")\n let nLabels = cellTitleLines.count\n\n cell.layer.borderWidth = 1\n cell.layer.cornerRadius = 8\n let labelHeight = cellHeight / cellTitleLines.count\n for i in (0 ..< nLabels) {\n let frame = CGRect(x: 0, y: labelHeight * i, width: cellWidth, height: labelHeight)\n let label1 = UILabel(frame: frame)\n cell.backgroundColor = UIColor.lightGray\n label1.numberOfLines = 1\n label1.text = headerLabels[indexPath.row]\n label1.textAlignment = .center\n label1.textColor = UIColor.black\n label1.clipsToBounds = true\n label1.adjustsFontSizeToFitWidth = true\n label1.text = cellTitleLines[i]\n cell.addSubview(label1)\n }\n\n return cell\n }\n\n func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {\n collectionContainerView.scrollToItem(at:IndexPath(item: indexPath.item, section: 0), at: .centeredHorizontally, animated: false)\n\n return true\n }\n\n func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {\n let cell = collectionContainerView.cellForItem(at: indexPath)\n print("cell = \\(cell)")\n collectionContainerView.scrollToItem(at:IndexPath(item: indexPath.item, section: 0), at: .centeredHorizontally, animated: false)\n }\n\n\n func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {\n return true\n }\n\n\n func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {\n }\n\n\n\n override func prepare(for segue: UIStoryboardSegue, sender: Any?) {\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1114 次 |
| 最近记录: |