[英]Using CollectionView in UIView with xib file
使用UICollectionView的主要方法是以編程方式管理邏輯。
使用Interface Builder或以編程方式設計單元。
創建主視圖控制器,包括帶有集合視圖的xib(或故事板),並通過Interface Builder將其鏈接到關聯的類。 或者,您可以以編程方式將集合視圖添加到UIViewController
通過在父類之后聲明它們,使目標視圖控制器符合UICollectionViewDelegate
和UICollectionViewDataSource
協議:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { @IBOutlet weak var collectionView: UICollectionView! //... }
在viewDidLoad
方法中為您的單元注冊關聯的nib或類,並將數據源和委托協議關聯到視圖控制器類:
let cellIdentifier = "cellIdentifier" override func viewDidLoad() { super.viewDidLoad() //if you use xibs: self.collectionView.register(UINib(nibName:"MyCollectionCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier) //or if you use class: self.collectionView.register(MyCollectionCell.self, forCellWithReuseIdentifier: cellIdentifier) self.collectionView.delegate = self self.collectionView.dataSource = self }
實現UICollectionViewDelegate
和UICollectionViewDataSource
協議中聲明的方法:
let objects = ["Cat", "Dog", "Fish"] func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.objects.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! MyCollectionCell //in this example I added a label named "title" into the MyCollectionCell class cell.title.text = self.objects[indexPath.item] return cell }
在模擬器中(或在真實設備上)運行您的應用程序和...évoilà! :)
有關詳細信息,請訪問: https : //developer.apple.com/reference/uikit/uicollectionview
好的,首先你必須擁有集合視圖的IBOutlet並實現這樣的方法
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
@IBOutlet var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
count = 9;
let nib = UINib(nibName: "yourItemView", bundle: nil)
collectionView.registerNib(nib, forCellWithReuseIdentifier: "yourItemView")
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
確定在添加xib文件的函數中,接下來必須從UICollectionViewCell創建該擴展,當你完成此操作時,必須覆蓋下一個方法
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return count
// the numbers of items
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {//size of your item for screen sizes
let wsize = UIScreen.mainScreen().bounds.size.width
switch(wsize){
case 414:
return CGSize(width: 190, height: 102)
case 375:
return CGSize(width: 190, height: 102)
case 320:
return CGSize(width: 174, height: 102)
default:
return CGSize(width: 174, height: 102)
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("yourItemView", forIndexPath: indexPath) as! yourItemView
return cell
}
這就是全部,祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.