[英]Take collectionview delegate and datasource out of view controller
I should start of with saying that I'm fairly new to swift. 我首先要说我是新手。 I'm trying to keep my view controllers as clean as possible.
我试图使我的视图控制器尽可能保持清洁。 I created the following class
我创建了以下课程
class AlbumArtist: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {
var musicLib = musicLibrary()
var albumCollection: UICollectionView!
var layout: CustomCollectionViewFlow!
init(albumCView: UICollectionView){
let nib = UINib(nibName: "CollectionViewCell", bundle: nil)
albumCollection = albumCView
musicLib.loadAlbums()
layout = CustomCollectionViewFlow()
print(layout)
super.init(frame: albumCollection.frame, collectionViewLayout: layout)
albumCollection.registerNib(nib, forCellWithReuseIdentifier: "item")
}
required init?(coder: NSCoder) {
musicLib.loadAlbums()
layout = CustomCollectionViewFlow()
super.init(coder: coder)
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return musicLib.getAlbumsCount()
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: CollectionViewCell = albumCollection.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath) as! CollectionViewCell
let objects = musicLib.getAlbums().objectAtIndex(indexPath.item) as! MPMediaItemCollection
let repObjects = objects.representativeItem
cell.lbl_Name.text = repObjects!.valueForProperty(MPMediaItemPropertyAlbumTitle) as? String
let artwork = repObjects!.valueForProperty(MPMediaItemPropertyArtwork)
let artworkImage = artwork?.imageWithSize(CGSize(width: 230, height: 230))
if(artworkImage != nil){
cell.img_artwork.image = artworkImage
}
else{
cell.img_artwork.image = UIImage(named: "no_Artwork.png")
}
return cell
}
} }
However I'm getting an error on my init method. 但是,我的初始化方法遇到错误。 Must call a designated initializer of the superclass 'UICollectionView'
必须调用超类'UICollectionView'的指定初始化程序
I'm not sure if it is correct what I'm doing I couldn't find any examples on how to do this. 我不确定我在做什么是否正确,我找不到任何有关如何执行此操作的示例。 So if I'm totally wrong I would love to hear what I need to do instead.
因此,如果我完全错了,我很想听听我需要做些什么。
EDIT: I updated my code and it's running now. 编辑:我更新了我的代码,它现在正在运行。 However it never comes to the delegate methods so the collection view is never being filled.
但是,它永远不会涉及委托方法,因此永远不会填充集合视图。 I updated the code in the post with the new code.
我用新代码更新了帖子中的代码。 In my viewController i'm calling the class above like this in my viewDidLoad
在我的viewController中,我像这样在viewDidLoad中调用上面的类
AlbumArtist(albumCView: AlbumCollectionView)
I'm not sure if what I'm doing is correct 我不确定我在做什么
You're trying to inherit from the UICollectionView
class, it not has any problem at all, but there are several ways to create a UICollectionView
without inherit from the class UICollectionView
: 你试图从继承
UICollectionView
类,它不会有任何问题都没有,但有几个方法可以创建UICollectionView
没有从类继承UICollectionView
:
@IBOutlet
to a UICollectionView
inside your UIViewController
using Interface Builder. @IBOutlet
到UICollectionView
你里面UIViewController
使用Interface Builder。 UICollectionViewController
to manage all using Interface Builder UICollectionViewController
以使用Interface Builder管理所有 UIContainerView
inside your UIViewController
to put inside an UICollectionViewController
. UIViewController
创建一个UIContainerView
以将其UICollectionViewController
。 I hope this help you. 希望对您有所帮助。
Apple defines a designated initializers as "the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain." Apple将指定的初始化程序定义为“类的主要初始化程序。指定的初始化程序会完全初始化该类引入的所有属性,并调用适当的超类初始化程序以继续超类链中的初始化过程。”
Basically for UICollectionView, instead of calling just super.init() which isn't a designated initializer, you should call super.init(frame: CGRect, collectionViewLayout: UICollectionViewLayout) 基本上,对于UICollectionView,应该调用super.init(frame:CGRect,collectionViewLayout:UICollectionViewLayout)而不是仅调用不是指定初始化程序的super.init()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.