我在storyboard.rootview
屏幕上有两个屏幕,就像启动画面一样,另一个是使用带标题的集合视图创建的主屏幕。
在标题中我点击了UIButton
,我应该可以显示我的应用程序的菜单选项,这也是一个集合视图。因为我对iOS编程比较新,所以我跟着a video in youtube
问题是根据视频我能够创建一个集合视图作为子视图但是在实现了collecton视图功能并运行应用程序后无法查看我配置的单元格
viewcontroller.swift
的代码就像这样
import UIKit
class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate{
var tableLabel:[String]=[""]
var tableImage: [String]=[""]
override func viewDidLoad() {
super.viewDidLoad()
}
let settingLauncher=SettingLauncher()
@IBAction func menuButtonTaped(sender: UIButton) {
settingLauncher.showSettings()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tableLabel.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:CellCollectionViewCell=collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CellCollectionViewCell
cell.layer.cornerRadius=10
cell.labelOne.text=tableLabel[indexPath.row]
cell.imageOne.image=UIImage(named:tableImage[indexPath.row])
cell.imageOne.layer.cornerRadius=41
cell.imageOne.layer.masksToBounds=true
return cell
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView
{
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "HomeHeader", forIndexPath: indexPath) as! CellCollectionReusableView
return header
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
和settingLauncher.swift
文件就像这样
import UIKit
class SettingLauncher:NSObject,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
override init() {
super.init()
collectionView.dataSource=self
collectionView.delegate=self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
}
let blackView=UIView()
var collectionView:UICollectionView={
let layout = UICollectionViewLayout()
let cv=UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor=UIColor.blueColor()
return cv
}()
let cellId="cellId"
func showSettings() {
if let window = UIApplication.sharedApplication().keyWindow{
blackView.backgroundColor=UIColor(white: 0,alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissShade)))
window.addSubview(blackView)
window.addSubview(collectionView)
let height:CGFloat=200
let y:CGFloat=window.frame.height-height
collectionView.frame=CGRectMake(0, window.frame.height, window.frame.width, height)
blackView.frame=window.frame
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: {
self.collectionView.frame=CGRectMake(0, y, self.collectionView.frame.width, self.collectionView.frame.height)
}, completion: nil)
}
}
func dismissShade(){
UIView.animateWithDuration(0.5){
self.blackView.alpha=0
if let window = UIApplication.sharedApplication().keyWindow{
self.collectionView.frame=CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height)
}
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
cell.backgroundColor=UIColor.redColor()
return cell
}
}
app构建成功没有任何错误,但我无法查看单击菜单按钮后创建的集合中的单元格
点击菜单
之前的屏幕截图
点击菜单
后的屏幕截图
答案 0 :(得分:0)
您应该将collectionView
dataSource
和delegate
设置为ViewController
课程中的viewController(就像您对SettingLauncher
课程所做的那样)。