Ios 敏捷的如何将UICollectionView写入独立的swift文件而不是ViewController

Ios 敏捷的如何将UICollectionView写入独立的swift文件而不是ViewController,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我找到了一套关于如何使用UICollectionView的教程。但他们只是在ViewController中编写它们。我想知道如何在另一个swift文件中编写UICollectionView。然后在我的ViewController类中调用它。这是我的UICollectionView代码 import UIKit class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewD

我找到了一套关于如何使用UICollectionView的教程。但他们只是在ViewController中编写它们。我想知道如何在另一个swift文件中编写UICollectionView。然后在我的ViewController类中调用它。这是我的UICollectionView代码

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var collectionView: UICollectionView!
    var ImageNames: [String] = ["photo", "photo1", "photo2", "photo3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 90, height: 120)

        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView)
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ImageNames.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        let imageView = UIImageView(image: UIImage(named: ImageNames[indexPath.row]))
        cell.backgroundView = imageView
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell?.backgroundView = nil
    }

}
顺便提一下,第二个问题。在我的代码中,我当前通过使数组全局化,将数组传递给
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
函数。如何在本地将ImagesNames数组传递给此函数?

重要的部分是

collectionView.dataSource = self
collectionView.delegate = self
它告诉您的集合视图在哪里可以找到它的数据源/委托方法,例如
cellForItemAtIndexPath

您应该创建一个具有
UICollectionViewDataSource
UICollectionViewDelegate
的新类,并在视图控制器中创建它的惰性变量

所以新班级

class MyDataController: NSObject, UICollectionViewDataSource, UICollectionViewDelegate {

  // all collection view stuff here

}
然后在ViewController中

lazy var dataController: MyDataController = {

    let dataController = MyDataController()

    return dataController
}()
最后

collectionView.dataSource = self
collectionView.delegate = self
变成

collectionView.dataSource = self.dataController
collectionView.delegate = self.dataController
关于第二个问题,您需要仔细考虑如何使用MVC约定存储数据。您可以在dataController中使用一个本地数组变量来控制collectionView中显示的信息,但是如何全局获取和保留该信息是一个广泛的主题

**编辑**

要跟进为什么懒惰的问题

override func viewDidLoad() {
    super.viewDidLoad()

    let dataController:MyDataController = MyDataController()
    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 90, height: 120)

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(collectionView)
}
变成了,取决于你的选择

override func viewDidLoad() {

    super.viewDidLoad()
    self.view.addSubview(collectionView)
}

// all other code

// then right at the bottom of your class

// MARK: - Properties

lazy var collectionView: UICollectionView = {

    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 90, height: 120)

    let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.dataSource = self.dataController
    collectionView.delegate = self.dataController
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.backgroundColor = UIColor.whiteColor()

    return collectionView
}()

lazy var dataController: MyDataController = {

    let dataController = MyDataController()
    dataController.delegate = self
    // more setup if needed

    return dataController
}()        
我发现这很方便,你的财产总是很容易找到,很容易移动,很容易改变。如果我注释掉
viewdiload
中的
addSubview
行,在这种情况下,这足以停止一起初始化
collectionView
dataController
。对于这个例子来说,这可能有些过分,但整体上采用它可以节省大量时间并生成清晰可读的oo代码。

重要的部分是

collectionView.dataSource = self
collectionView.delegate = self
它告诉您的集合视图在哪里可以找到它的数据源/委托方法,例如
cellForItemAtIndexPath

您应该创建一个具有
UICollectionViewDataSource
UICollectionViewDelegate
的新类,并在视图控制器中创建它的惰性变量

所以新班级

class MyDataController: NSObject, UICollectionViewDataSource, UICollectionViewDelegate {

  // all collection view stuff here

}
然后在ViewController中

lazy var dataController: MyDataController = {

    let dataController = MyDataController()

    return dataController
}()
最后

collectionView.dataSource = self
collectionView.delegate = self
变成

collectionView.dataSource = self.dataController
collectionView.delegate = self.dataController
关于第二个问题,您需要仔细考虑如何使用MVC约定存储数据。您可以在dataController中使用一个本地数组变量来控制collectionView中显示的信息,但是如何全局获取和保留该信息是一个广泛的主题

**编辑**

要跟进为什么懒惰的问题

override func viewDidLoad() {
    super.viewDidLoad()

    let dataController:MyDataController = MyDataController()
    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 90, height: 120)

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(collectionView)
}
变成了,取决于你的选择

override func viewDidLoad() {

    super.viewDidLoad()
    self.view.addSubview(collectionView)
}

// all other code

// then right at the bottom of your class

// MARK: - Properties

lazy var collectionView: UICollectionView = {

    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 90, height: 120)

    let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.dataSource = self.dataController
    collectionView.delegate = self.dataController
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.backgroundColor = UIColor.whiteColor()

    return collectionView
}()

lazy var dataController: MyDataController = {

    let dataController = MyDataController()
    dataController.delegate = self
    // more setup if needed

    return dataController
}()        

我发现这很方便,你的财产总是很容易找到,很容易移动,很容易改变。如果我注释掉
viewdiload
中的
addSubview
行,在这种情况下,这足以停止一起初始化
collectionView
dataController
。对于这个示例来说,这可能有些过分,但整体上采用它可以节省大量时间并生成清晰可读的oo代码。

为什么懒惰?似乎
让dataController=MyDataController()
更可取,除非您认为集合视图有时会出现,否则我更喜欢lazy,因为我个人认为它很整洁。它可以让你在需要的时候利用变量,而且没有任何危害,也许将来会有一些变化,我发现这真的是易读且容易改变。。。。你是对的,虽然我们不需要。。。我们还可以将
collectionView
设置为惰性,并将其粘贴在
dataController
中。。总是一个偏好的问题。。。Lazy var是我比较喜欢的风格,提问者问了一个与风格相关的问题。我发现在
viewDidLoad
中粘贴所有内容变得很麻烦。我同意,但值得注意的是
让dataController=MyDataController()
不需要在
viewDidLoad
中。几乎可以肯定,将它与
UICollectionViewFlowLayout
放在一起很好,但您可能需要一个指向它的指针来稍后访问它。我刚才说的是将它定义为属性为什么懒惰?似乎
让dataController=MyDataController()
更可取,除非您认为集合视图有时会出现,否则我更喜欢lazy,因为我个人认为它很整洁。它可以让你在需要的时候利用变量,而且没有任何危害,也许将来会有一些变化,我发现这真的是易读且容易改变。。。。你是对的,虽然我们不需要。。。我们还可以将
collectionView
设置为惰性,并将其粘贴在
dataController
中。。总是一个偏好的问题。。。Lazy var是我比较喜欢的风格,提问者问了一个与风格相关的问题。我发现在
viewDidLoad
中粘贴所有内容变得很麻烦。我同意,但值得注意的是
让dataController=MyDataController()
不需要在
viewDidLoad
中。几乎可以肯定,将它与
UICollectionViewFlowLayout
放在一起很好,但您可能需要一个指向它的指针来稍后访问它。我刚才说的是将它定义为属性