2016-12-31 4 views
4

私はスタンドアローンで動作する単純なコレクションビューテスト(オンラインチュートリアルに基づいています)を持っています。しかし、それをナビゲーションコントローラに埋め込むと、動作が停止します。私は、(1)headerView(64ピクセル高)を作成し、それを上のビューに追加することでコードを画面に構築しました。 (2)コレクションビューを作成してheaderViewに追加しました。ここでナビゲーションコントローラに埋め込まれているとCollectionViewが機能しません

はコードです:

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. 
} 

}

+0

I 64(単一の行を強制的に)サブビューとしてコレクションビューを有する上面の高さを制限していました。トップビューの高さを128に増やすと、セルが2番目の行に表示されます。ナビゲーションコントローラがなぜセルを下の行に強制することと関係があるのか​​はわかりません。どんな指導も高く評価されます。 –

+0

collectionview = UICollectionView(フレーム:CGRect.zero、collectionViewLayout:layout)collectionView.autoresizingMask = [.flexibleLeftMargin、.flexibleLeftMargin、.flexibleBottomMargin、.flexibleRightMargin、.flexibleHeight、 .flexibleWidth] collectionView.setNeedsDisplay()// viewdidAppearのすべてのコレクションビューコードを移動します。 – kaushal

答えて

0

述べたように、私はKaushalの提案の作品を作ることができませんでした。コレクションビューを埋め込んだビューの位置付けが、私が理解していない理由でviewDidLoadに間違って配置されていたという手がかりを与えました。しかし、(viewDidLoadではなく)viewDidAppearにコレクションビューの設定を入れることでうまくいきました。私はnavbarをクリアするためにyの位置を64だけオフセットし、行の高さを64に減らしました。また、コードを一度しか実行しないようにして、ページからのナビゲーションが互いに重なり合う複数のビューを追加しないようにします。ところで、私の本来の目的は、水平方向にスクロールすることでした。私のプログラムでは、対応するセクションを持つテーブルビューがあり、その考え方は、水平方向にスクロールしている行を使用して、対応するセクションに移動することです。

コードを以下に示す:

// 
// CustomViewController.swift 
// DSM Tracker 
// 
// Created by Syed Tariq on 1/7/17. 
// Copyright © 2017 com.syedtariq. All rights reserved. 
// 

import UIKit 


class ViewController: UIViewController, 
    UICollectionViewDelegate, 
    UICollectionViewDataSource, 
    UINavigationControllerDelegate 

{ 

    var executeOnce = true 
    var cellDimensions = [String:Int]() 
    var cellHeight = 50 
    var cellWidth = 120 

    var collectionContainerView: UICollectionView! 
    var navBar: UINavigationBar = UINavigationBar() 


    // view constants 
    var viewY = CGFloat() 
    var viewX = CGFloat() 
    var viewWidth = CGFloat() 
    var viewHeight = CGFloat() 


    // gaps from view edge 
    let leftGap = CGFloat(20) 
    let rightGap = CGFloat(20) 


    // navbar constants 
    let navBarHeight = CGFloat(64) 

    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"] 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     navBar.backgroundColor = UIColor.green 
     executeOnce = true 
     viewY = view.frame.origin.y 
     viewX = view.frame.origin.x 
     viewWidth = view.frame.width 
     viewHeight = view.frame.height 
    } 



    func configureCollectionView() { 
     if executeOnce { 
      executeOnce = false 
      let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
      layout.scrollDirection = .horizontal 
      layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10) 
      layout.itemSize = CGSize(width: cellWidth, height: cellHeight) 

      let colWidth = viewWidth - leftGap - rightGap 
      let colX = viewX + leftGap 
      let colY = viewY + navBarHeight 
      let colHeight = CGFloat(64) 
      let frame = CGRect(x:colX, y:colY, width: colWidth, height: colHeight) 
      //let frame = CGRect.zero 
      collectionContainerView = UICollectionView (frame: frame, collectionViewLayout: layout) 
      collectionContainerView.dataSource = self 
      collectionContainerView.delegate = self 
      collectionContainerView.autoresizingMask = [.flexibleLeftMargin,.flexibleLeftMargin,.flexibleBottomMargin,.flexibleRightMargin, .flexibleHeight, .flexibleWidth] 
      collectionContainerView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell") 
      collectionContainerView.backgroundColor = UIColor.blue 
      collectionContainerView.allowsSelection = true 
      collectionContainerView.isScrollEnabled = true 
      collectionContainerView.setNeedsDisplay() 
      print("collectionContainerView.frame \(collectionContainerView.frame)") 
      view.addSubview(collectionContainerView) 
     } 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     configureCollectionView() 
    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     print("headerLabels.count \(headerLabels.count)") 
     return headerLabels.count 
    } 



    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() 
     } 

     let cellTitle = headerLabels[indexPath.row] 
     let cellTitleLines = cellTitle.components(separatedBy: " ") 
     let nLabels = cellTitleLines.count 

     cell.layer.borderWidth = 1 
     cell.layer.cornerRadius = 8 
     let labelHeight = cellHeight/cellTitleLines.count 
     for i in (0 ..< nLabels) { 
      let frame = CGRect(x: 0, y: labelHeight * i, width: cellWidth, height: labelHeight) 
      let label1 = UILabel(frame: frame) 
      cell.backgroundColor = UIColor.lightGray 
      label1.numberOfLines = 1 
      label1.text = headerLabels[indexPath.row] 
      label1.textAlignment = .center 
      label1.textColor = UIColor.black 
      label1.clipsToBounds = true 
      label1.adjustsFontSizeToFitWidth = true 
      label1.text = cellTitleLines[i] 
      cell.addSubview(label1) 
     } 

     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
     collectionContainerView.scrollToItem(at:IndexPath(item: indexPath.item, section: 0), at: .centeredHorizontally, animated: false) 

     return true 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let cell = collectionContainerView.cellForItem(at: indexPath) 
     print("cell = \(cell)") 
     collectionContainerView.scrollToItem(at:IndexPath(item: indexPath.item, section: 0), at: .centeredHorizontally, animated: false) 
    } 


    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool { 
     return true 
    } 


    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) { 
    } 



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    } 
} 
関連する問題