简体   繁体   English

将CollectionView委托和数据源带出视图控制器

[英]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

  • You can just create an @IBOutlet to a UICollectionView inside your UIViewController using Interface Builder. 你可以创建一个@IBOutletUICollectionView你里面UIViewController使用Interface Builder。
  • Create an UICollectionViewController to manage all using Interface Builder 创建一个UICollectionViewController以使用Interface Builder管理所有
  • Create an 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.

相关问题 如何在View Controller外部设置数据源和委托 - How to set datasource and delegate Outside of View Controller 当数据源和委托与View Controller分开时,如何将Table View的单击项发送到其他View Controller - How to send clicked item of Table View to other view controller when datasource and delegate are separate from View Controller 具有委托关系的先前视图控制器未采取预期的操作 - Previous view controller with delegate relationship does not take expected action 即使视图控制器是“子”控制器,如何使用委托和数据源更新UITableView? - How to make UITableView updating with delegate and datasource even if the view controller is a “child” controller? 为扩展视图添加委托和数据源 - Add delegate and dataSource for extension view 第二个集合视图的委托和数据源 - Delegate and Datasource for second Collection View 视图控制器内的UITable视图,不要随委托和数据源实现而变化 - UITable view inside view controller , don't change with delegate and datasource implements 自定义单元格类中的init(),它是collectionView的委托和数据源 - init() inside custom cell class that is delegate and datasource of collectionView 我该如何设置为数据源并为表中的collectionView委托 - what do I set as the Datasource and delegate for a collectionView within a table 未在View Controller中加载CollectionView单元 - CollectionView Cells not loading in View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM