迅速。如何在ViewController以外的单独swift文件中编写UICollectionView

时间:2016-05-03 16:48:10

标签: ios swift uicollectionview

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

顺便说一下,第二个问题。在我的代码中,我目前通过使数组全局传递数组到result = result.Where(i => i.OptionKey.ToString().ToList().Contains(a)); 函数。如何在本地将ImagesNames数组传递给此函数??。

1 个答案:

答案 0 :(得分:6)

重要的部分是

collectionView.dataSource = self
collectionView.delegate = self

告诉您的收藏集查看在哪里找到它的数据源/委托方法,例如cellForItemAtIndexPath

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

所以新课......

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
}()        

我觉得这很方便,您的属性总是很容易找到,易于移动并且易于更改。如果我在addSubview中注释掉viewDidLoad行,在这种情况下足以阻止collectionViewdataController一起初始化。也许这个例子有点过分,但从整体上看,它可以节省大量时间并产生干净的可读oo代码。