2017-08-21 6 views
0

コードではなく2つのUICollectionViewを使用してビューを作成しました。ストーリーボードでそれらを作成し、コードでそれらを見つける必要があります。 これは私がそれをやった方法です:*コード別ストーリーボードビューの配置

キャッチされない例外のために終了アプリ「NSInvalidArgumentException」、理由:

 TopProducts = UICollectionView(frame: CGRect(x: 8, y: scrollViewHeight + 166, width: viewWidth - 8, height: 120)) 
    contentScrollView.addSubview(TopProducts) 

アプリケーションがクラッシュし、このエラーがコンソールに表示さ「UICollectionViewでなければなりません*非nilのレイアウトパラメータ」 で初期化まずスローコールスタック: ( 0 CoreFoundationの0x000000010271526b exceptionPreprocess + 171 1 libobjc.A.dylib 0x0の00000010160af41 objc_exception_throw + 48 2 CoreFoundationのを0x0000000102789ba5 + [NSExceptionレイズ:フォーマット:] + 197 3のUIKit 0x0000000105d14a39 - [UICollectionView initWithFrame:collectionViewLayout:] + 81 4のUIKit 0x0000000105d149e2 - [UICollectionView initWithFrame:] + 58 5 JahanCoカタログは_T0So16UICollectionViewCABSC6CGRectV5frame_tcfcTOを0x0000000100747bfd + 77 6 JahanCoカタログ0x0000000100744f24 _T0So16UICollectionViewCABSC6CGRectV5frame_tcfC + 100 7 JahanCoカタログ0x00000001007446f0 _T015JahanCo_Catalog9SlideViewC11viewDidLoadyyF + 9936 8 JahanCoカタログ0x0000000100744fa4 _T015JahanCo_Catalog9SlideViewC11viewDidLoadyyFTo + 36 9のUIKit 0x000000010542fd93 - [のUIViewController loadViewI fRequired] + 1235 10のUIKit 0x0000000105476cd4 - [UINavigationController _updateScrollViewFromViewController:toViewController:] + 68 11のUIKit 0x0000000105477010 - [UINavigationController _startTransition:fromViewController:toViewController:] + 153 12のUIKit 0x0000000105478127 - [UINavigationController _startDeferredTransitionIfNeeded:] + 841 13のUIKit 0x0000000105479388 - [UINavigationController __viewWillLayoutSubviews] + 115 14のUIKit 0x00000001056c26d9 - [UILayoutContainerView layoutSubviews] + 231 15のUIKit 0x000000010536321e - [UIViewの(CALayerDelegate)layoutSublayersOfLayer:] + 1331 16 QuartzCore 0x0000000103375c92 - [CALayerのlayoutSublayers] + 153 17 QuartzCore 0x0000000103379d79 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401 18 QuartzCore 0x0000000103302851 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 385 19 QuartzCore 0x000000010332e1c2 _ZN2CA11Transaction6commitEv + 500 20のUIKit 0x00000001052b11de __34-【のUIApplication _firstCommitBlock] _block_invoke_2 + 141 21 CoreFoundationのを0x00000001026b82acの__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 12 22 CoreFoundationの0x000000010269cadb __CFRunLoopDoBlocks + 203 23 CoreFoundationの0x000000010269c2b4 __CFRunLoopRun + 1300 24 CoreFoundation 0x000000010269bb29 CFRunLoopRunSpecific + 409 25 GraphicsServices 0x000000010aa059c6 GSEventRunModal + 62 2 6 UIKit 0x00000001052959a4 UIApplicationMain + 159 27 JahanCoカタログ0x0000000100754ed7メイン+ 55 28 libdyld.dylib 0x000000010750f621 start + 1 29 ??? 0x0000000000000001 0x0 + 1 ) libC++ abi。dylib:タイプのキャッチされない例外NSException で終了する(lldb)

+0

CollectionViewを作成する方法でありますトッププロダクトを宣言しましたか? –

+0

UICollectionViewはflowLayoutを必要とします – junaidsidhu

+0

あなたはコードを通してstoryBoardを使って追加されたcollectionViewの制約を変更したいですか? –

答えて

1

後は、プログラムにより

あなたが持っている
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) 
} 

} 
+0

ありがとうございます。どのように水平方向のスクロール方向に設定するのですか? – VahidGR

+0

@VahidGRあなたはflowLayout.scrollDirection = .horizo​​ntalを使うことができると思います – 3stud1ant3

1
HERESに小さなコードスニペット

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 
    } 
} 
関連する問題