我已经通过代码创建了我的视图,但是有两个UICollectionViews。所以在故事板中创建了它们,并需要通过代码找到它们。我就是这样做的:
TopProducts = UICollectionView(frame: CGRect(x: 8, y: scrollViewHeight + 166, width: viewWidth - 8, height: 120))
contentScrollView.addSubview(TopProducts)应用程序崩溃,此错误出现在控制台中:
*终止应用程序,原因是“NSInvalidArgumentException”异常,原因:“必须使用非零布局参数初始化UICollectionView”*第一次抛出调用堆栈:(0 CoreFoundation 0x000000010271526b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010160af41 objc_exception_throw + 48 2 CoreFoundation 0x0000000102789ba5 +NSException :format:+ 197 3 UIKit )0x0000000105d14a39 _T0So16UICollectionViewCABSC6CGRectV5frame_tcfcTO initWithFrame:-UICollectionView initWithFrame:+ 81 4 UIKit 0x0000000105d149e2 -UICollectionView initWithFrame:+585 JahanCo Catalog 0x00000100747bfd-UICollectionView+776 JahanCo Catalog 0x0000000100744f24 _T0So16UICollectionViewCABSC6CGRectV5frame_tcfC + 100 7 JahanCo Catalog 0x000001007446f0 _T015JahanCo_Catalog9SlideViewC11viewDidLoadyyF + 9936 8 JahanCo目录0x0000000100744fa4 _T015JahanCo_Catalog9SlideViewC11viewDidLoadyyFTo + 36 9 UIKit 0x0000010542fd93 -UIViewController loadViewIfRequired + 1235 10 UIKit 0x00000105476cd4 -UINavigationController _updateScrollViewFromViewController:toViewController:+ 68 11 UIKit 0x00000105477010 -UINavigationController _startTransition:fromViewController:toViewController:+ 153 12 UIKit-UINavigationController _startDeferredTransitionIfNeeded:+ 841 13 UIKit 0x0000000105479388 -UINavigationController __viewWillLayoutSubviews + 115 14 UIKit 0x000001056c26d9 -UILayoutContainerView layoutSubviews + 231 15 UIKit 0x0000010536321e -UIView(CALayerDelegate) layoutSublayersOfLayer:+ 1331 16 QuartzCore 0x0000000103375c92 -CALayer layoutSublayers+ 153 17 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE 0x00000103379d79 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 401 18 QuartzCore 0x0000000103302851 _ZN2CA7Context18commit_transactionEPNS_11TransactionE+ 385 19 QuartzCore 0x00000010332e1c2 _ZN2CA11Transaction6commitEv + 500 20 UIKit 0x000001052b11de34-UIApplication _firstCommitBlock_block_invoke_2 + 141 21 CoreFoundation0x00000001026b82ac __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 12 22 CoreFoundation 0x0000010269cadb __CFRunLoopDoBlocks + 203 23 CoreFoundation 0x0000010269c2b4 __CFRunLoopRun + 1300 24 CoreFoundation 0x0000010269bb29 CFRunLoopRunSpecific + 409 25 GraphicsServices 0x000000010a059c6 GSEventRunModal + 62 26 UIKit0x00000001052959a4 UIApplicationMain + 159 27 JahanCo Catalog 0x0000000100754ed7 main + 55 28 libdyld.dylib 0x000000010750f621 start +1 29 ?0x00000000000001 0x0 +1) libc++abi.dylib:以NSException (lldb)类型的不明确例外终止
发布于 2017-08-21 07:32:55
下面是以编程方式创建CollectionView的方法
class GridViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let flowLayout = UICollectionViewFlowLayout()
//to set the scroll direction
flowLayout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.cyan
self.view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.green
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: 50, height: 50)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
}发布于 2017-08-21 07:35:58
下面是一个小代码片段
import Foundation
import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.blue
self.view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
}https://stackoverflow.com/questions/45791486
复制相似问题