๐ป Easy CollectionViewCell Controller
columnCount ์ซ์์ ๋ฐ๋ผ ๊ฐ๋ก ๊ฐฏ์๊ฐ ์ ํด์ง๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฆฌ๋๋ทฐ width ๊ธฐ๋ฐ์ผ๋ก cell์ width๊ฐ ์๋์ผ๋ก ๋์ด๋จ (๊ฐ๋ก์ ๋ํ ๋์ด ๊ฐ๋ณ ์ฒ๋ฆฌ๋ getGridViewHeight ์ด์ฉํ์ฌ ์ฒ๋ฆฌ ๊ฐ๋ฅํจ)
columnCount == 0 ์ผ๋ก ์ ํ ์ Cell์ Xib์ Width ํฌ๊ธฐ๋ก๋๋ก Cell์ ๊ฐ๋ก ์ซ์๊ฐ ๊ฐ๋ณ์ผ๋ก ์ง์ ๋์ด ๊ทธ๋ ค์ง ๊ทธ๋ฆฌ๋๋ทฐ ๊ฐ๋ก ์ฌ์ด์ฆ๋ฅผ ๋์ด๊ฐ๋ฉด ํ๋จ์ผ๋ก ๋ด๋ ค๊ฐ๊ฐ
GirdView Xib์์ Cell์ ์ด๋ฆ์ด๋ ์ฌ๋ ค๊ฐ์ง ์ ํ ์ด ๊ฐ๋ฅํ๋ฏ๋ก ์ฝ๋๊ฐ ๊ฑฐ์ ์์
let texts = ["ํ
์คํธ1","ํ
์คํธ2","ํ
์คํธ3","ํ
์คํธ4","ํ
์คํธ5","ํ
์คํธ6","ํ
์คํธ7","ํ
์คํธ8","ํ
์คํธ9"]
let data = GridViewData(itemList: texts)
gridView.configure(data: data) { (name, object) in
print(name)
}
protocol PkhGridViewCellProtocol: UICollectionViewCell {
var actionClosure: OnActionClosure? { get set }
static func getGridViewHeight(data: Any?, width: CGFloat) -> CGFloat
func configure(_ data: Any?)
}
@IBInspectable var cellName: String {
get {
return cellType.className
}
set {
cellType = (swiftClassFromString(newValue).self as! PkhGridViewCellProtocol.Type)
}
}
@IBInspectable var columnCount: Int = 0 {
didSet {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
layout.columnCount = columnCount
}
}
}
@IBInspectable var columnSpacing: CGFloat {
get {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
return layout.minimumColumnSpacing
}
return 0
}
set {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
layout.minimumColumnSpacing = newValue
}
}
}
@IBInspectable var lineSpacing: CGFloat {
get {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
return layout.minimumInteritemSpacing
}
return 0
}
set {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
layout.minimumInteritemSpacing = newValue
}
}
}
@IBInspectable var showLineCount: Int = 1 {
didSet {
guard let data = self.data, data.itemList.count > 0 else { return }
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
layout.invalidateLayout()
self.frame.size.height = self.showLineHeight
}
}
}
@IBInspectable var heightFit: Bool = true
@IBInspectable var isShowBottomLine: Bool {
get {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
return layout.isShowBottomLine
}
return false
}
set {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
layout.isShowBottomLine = newValue
}
}
}
@IBInspectable var bottomLineColor: UIColor {
get {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
return layout.bottomLineColor
}
return UIColor.gray
}
set {
if let layout = self.collectionView.collectionViewLayout as? GridViewFlowLayout {
layout.bottomLineColor = newValue
}
}
}
@IBInspectable public var topInset: CGFloat {
get { return sectionInset.top }
set { sectionInset.top = newValue }
}
@IBInspectable public var bottomInset: CGFloat {
get { return sectionInset.bottom }
set { sectionInset.bottom = newValue }
}
@IBInspectable public var leftInset: CGFloat {
get { return sectionInset.left }
set { sectionInset.left = newValue }
}
@IBInspectable public var rightInset: CGFloat {
get { return sectionInset.right }
set { sectionInset.right = newValue }
}