手动将委托和数据源添加到 xib 文件的 UICollectionView
Manually add delegate & datasource to UICollectionView of xib file
我把UICollectionView放到了xib文件的UIView上。
在xib文件中安装(GUI)UICollectionView,连接IBOutlet
这样的话,delegate和dataSource应该怎么写呢?
(猜想:也许,我得自己手动写成代码)
此外,我们无法将 Cell 附加到 xib 中的 UICollectionView。
这是否意味着我不必放它?
Collection.swift
//Collection.swift
import UIKit
class Home: UIView, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet weak var propCollectionView: UICollectionView!
let photos = ["sierra"] //Already put to AssetsFolder
//Just instance. plz do not care about this.
class func instance() -> Home {
return UINib(nibName: "Home", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! Home
}
func initValue(){
propCollectionView.register(UINib(nibName: "PropCell", bundle: nil), forCellWithReuseIdentifier: "PropCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = propCollectionView.dequeueReusableCell(withReuseIdentifier: "PropCell", for: indexPath) as! PropCell
let propImage = cell.contentView.viewWithTag(1) as! UIImageView
let img = UIImage(named: photos[indexPath.row])
propImage.image = img
return cell
}
}
PropCell.swift
//PropCell.swift
//yeah, nothing. plz don't care this too.
import UIKit
class PropCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
}
CollectionSwift 的 Xib
PropCell 的 Xib
(如有不明白题意,欢迎评论。)
import UIKit
class Home: UIView, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet weak var propCollectionView: UICollectionView!
let photos = ["sierra"] //Already put to AssetsFolder
//Just instance. plz do not care about this.
class func instance() -> Home {
return UINib(nibName: "Home", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! Home
}
func configureView() {
propCollectionView.delagate = self
propCollectionView.dataSource = self
}
func initValue(){
propCollectionView.register(UINib(nibName: "PropCell", bundle: nil), forCellWithReuseIdentifier: "PropCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = propCollectionView.dequeueReusableCell(withReuseIdentifier: "PropCell", for: indexPath) as! PropCell
let propImage = cell.contentView.viewWithTag(1) as! UIImageView
let img = UIImage(named: photos[indexPath.row])
propImage.image = img
return cell
}
}
用途
let home = Home.instance()
home.configureView() //called configureView() with the home instance
我把UICollectionView放到了xib文件的UIView上。
在xib文件中安装(GUI)UICollectionView,连接IBOutlet
这样的话,delegate和dataSource应该怎么写呢?
(猜想:也许,我得自己手动写成代码)
此外,我们无法将 Cell 附加到 xib 中的 UICollectionView。
这是否意味着我不必放它?
Collection.swift
//Collection.swift
import UIKit
class Home: UIView, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet weak var propCollectionView: UICollectionView!
let photos = ["sierra"] //Already put to AssetsFolder
//Just instance. plz do not care about this.
class func instance() -> Home {
return UINib(nibName: "Home", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! Home
}
func initValue(){
propCollectionView.register(UINib(nibName: "PropCell", bundle: nil), forCellWithReuseIdentifier: "PropCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = propCollectionView.dequeueReusableCell(withReuseIdentifier: "PropCell", for: indexPath) as! PropCell
let propImage = cell.contentView.viewWithTag(1) as! UIImageView
let img = UIImage(named: photos[indexPath.row])
propImage.image = img
return cell
}
}
PropCell.swift
//PropCell.swift
//yeah, nothing. plz don't care this too.
import UIKit
class PropCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
}
CollectionSwift 的 Xib
PropCell 的 Xib
(如有不明白题意,欢迎评论。)
import UIKit
class Home: UIView, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet weak var propCollectionView: UICollectionView!
let photos = ["sierra"] //Already put to AssetsFolder
//Just instance. plz do not care about this.
class func instance() -> Home {
return UINib(nibName: "Home", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! Home
}
func configureView() {
propCollectionView.delagate = self
propCollectionView.dataSource = self
}
func initValue(){
propCollectionView.register(UINib(nibName: "PropCell", bundle: nil), forCellWithReuseIdentifier: "PropCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = propCollectionView.dequeueReusableCell(withReuseIdentifier: "PropCell", for: indexPath) as! PropCell
let propImage = cell.contentView.viewWithTag(1) as! UIImageView
let img = UIImage(named: photos[indexPath.row])
propImage.image = img
return cell
}
}
用途
let home = Home.instance()
home.configureView() //called configureView() with the home instance