Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中使用另一个类的UICollectionView_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 在Swift中使用另一个类的UICollectionView

Ios 在Swift中使用另一个类的UICollectionView,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在尝试编写一个将显示collectionView的类。 我在尝试将collectionView作为子视图添加到调用类的UIView时遇到了问题。 这可能吗? 任何洞察都是值得赞赏的 ViewController: import UIKit class ViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() let editMenu = collection(vi

我正在尝试编写一个将显示collectionView的类。 我在尝试将collectionView作为子视图添加到调用类的UIView时遇到了问题。 这可能吗? 任何洞察都是值得赞赏的

ViewController:

import UIKit
class ViewController: UIViewController{
  override func viewDidLoad() {
    super.viewDidLoad()
    let editMenu = collection(view: view, xPos: 0, yPos: 0, width: view.frame.width, height: view.frame.height)
   }
}
和集合视图类:

import UIKit
class collection: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {  
  var collectionView: UICollectionView?
  let cellIdentifier = "CellIdentifier"
  var myView = UIView()
  var xPos, yPos, width, height: CGFloat 
  required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }  
  init(view: UIView, xPos: CGFloat, yPos: CGFloat, width: CGFloat, height: CGFloat) {
    self.myView = view
    self.xPos = xPos
    self.yPos = yPos
    self.width = width
    self.height = height
    super.init(nibName: nil, bundle: nil)
    configureCollection()    
  }
  func configureCollection()
  {
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 00, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 100, height: 80)   
    let frame = CGRectMake(xPos, yPos, width, height)
    collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
    collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
    collectionView!.backgroundColor = UIColor.redColor()
    collectionView!.dataSource = self
    collectionView!.delegate = self
    myView.addSubview(collectionView!)    //<- EXC_BAD_ACCESS    
  }
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
  } 
  func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 2
  }
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! UICollectionViewCell

    var textLabel = UILabel(frame: CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height))
    textLabel.textAlignment = NSTextAlignment.Center
    textLabel.textColor = UIColor.darkTextColor()
    textLabel.text = "Cell \(indexPath.row)"
    cell.contentView.addSubview(textLabel)

    return cell
  }
  func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    NSLog("selected \(indexPath.row)")
  }
}
导入UIKit
类集合:UIViewController、UICollectionViewDelegateFlowLayout、UICollectionViewDataSource{
var collectionView:UICollectionView?
让cellIdentifier=“cellIdentifier”
var myView=UIView()
变量xPos,yPos,宽度,高度:CGFloat
必需的初始化(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}  
init(视图:UIView,xPos:CGFloat,yPos:CGFloat,宽度:CGFloat,高度:CGFloat){
self.myView=view
self.xPos=xPos
self.yPos=yPos
self.width=宽度
自我高度=高度
super.init(nibName:nil,bundle:nil)
配置集合()
}
func configureCollection()
{
let布局:UICollectionViewFlowLayout=UICollectionViewFlowLayout()
layout.sectionInset=UIEDGEINSET(顶部:00,左侧:10,底部:10,右侧:10)
layout.itemSize=CGSize(宽:100,高:80)
let frame=CGRectMake(xPos、yPos、宽度、高度)
collectionView=UICollectionView(帧:帧,collectionViewLayout:layout)
collectionView!.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier:cellIdentifier)
collectionView!.backgroundColor=UIColor.redColor()
collectionView!.dataSource=self
collectionView!.delegate=self
myView.addSubview(collectionView!)//Int{
返回4
} 
func numberOfSectionsInCollectionView(collectionView:UICollectionView)->Int{
返回2
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier,forIndexPath:indexPath)作为!UICollectionViewCell
var textlab=UILabel(框架:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height))
textLabel.textAlignment=NSTextAlignment.Center
textlab.textColor=UIColor.darkTextColor()
textLabel.text=“Cell\(indexPath.row)”
cell.contentView.addSubview(textLabel)
返回单元
}
func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath){
NSLog(“选定\(indexPath.row)”)
}
}

我想说这真的很糟糕,如果您的collectionview包含在其他一些viewcontroller中,并且您希望将viewcontroller的视图添加到当前视图中,请使用UIViewController包含,使此视图控制器将collectionview作为子视图控制器为什么不使用容器视图?请详细说明您遇到过麻烦吗?谢谢您的评论。我将研究UIViewController包含并发布它的工作原理。到目前为止,我只有一个视图是以编程方式完成的。添加和隐藏uiButtons和ScrollView都很好。不确定这是否“正确”但直到我需要一个collectionView时,它才起作用。@k6sandeep感谢您建议使用包容和ChildViewController。最终,教程起到了帮助作用。