मैं यहाँ https://github.com/SebastienMichoy/CollectionViewsDemo/tree/master/CollectionViewsDemo/Sources/Collections%20Views
स्विफ्ट 3
उपवर्ग uicollectionreusableview
class SectionView: UICollectionReusableView {
static let kind = "sectionView"
}
उपवर्ग uicollectionViewFlowLayout
+०१२३५१६४१० इस रेपो के बंद हो गई
class CustomFlowLayout: UICollectionViewFlowLayout {
// MARK: Properties
var decorationAttributes: [IndexPath: UICollectionViewLayoutAttributes]
var sectionsWidthOrHeight: [IndexPath: CGFloat]
// MARK: Initialization
override init() {
self.decorationAttributes = [:]
self.sectionsWidthOrHeight = [:]
super.init()
}
required init?(coder aDecoder: NSCoder) {
self.decorationAttributes = [:]
self.sectionsWidthOrHeight = [:]
super.init(coder: aDecoder)
}
// MARK: Providing Layout Attributes
override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return self.decorationAttributes[indexPath]
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var attributes = super.layoutAttributesForElements(in: rect)
let numberOfSections = self.collectionView!.numberOfSections
var xOrYOffset = 0 as CGFloat
for sectionNumber in 0 ..< numberOfSections {
let indexPath = IndexPath(row: 0, section: sectionNumber)
let numberOfItems = self.collectionView?.numberOfItems(inSection: sectionNumber)
let sectionWidthOrHeight = numberOfItems == 0 ? UIScreen.main.bounds.height : collectionViewContentSize.height//self.sectionsWidthOrHeight[indexPath]!
let decorationAttribute = UICollectionViewLayoutAttributes(forDecorationViewOfKind: SectionView.kind, with: indexPath)
decorationAttribute.zIndex = -1
if self.scrollDirection == .vertical {
decorationAttribute.frame = CGRect(x: 0, y: xOrYOffset, width: self.collectionViewContentSize.width, height: sectionWidthOrHeight)
} else {
decorationAttribute.frame = CGRect(x: xOrYOffset, y: 0, width: sectionWidthOrHeight, height: self.collectionViewContentSize.height)
}
xOrYOffset += sectionWidthOrHeight
attributes?.append(decorationAttribute)
self.decorationAttributes[indexPath] = decorationAttribute
}
return attributes
}
}
इस
CollectionView प्रतिनिधि समारोह
func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
Log.printLog(identifier: elementKind, message: indexPath)
if elementKind == UICollectionElementKindSectionHeader, let view = view as? ProfileViewHeaderView {
view.backgroundColor = UIColor(red: (102/255.0), green: (169/255.0), blue: (251/255.0), alpha: 1)
} else if elementKind == SectionView.kind {
let evenSectionColor = UIColor.black
let oddSectionColor = UIColor.red
view.backgroundColor = (indexPath.section % 2 == 0) ? evenSectionColor : oddSectionColor
}
}
लागू यह महत्वपूर्ण collectionView नहीं है
let layout = CustomFlowLayout()
layout.register(SectionView.self, forDecorationViewOfKind: SectionView.kind)
लेआउट के साथ UICollectionReusableView रजिस्टर।
एक और बात। मैं layoutAttributesForElements में ऊंचाई के साथ गड़बड़ कर दिया। आपको इसे अपनी परियोजना के लिए बदलना चाहिए।
पूरक विचारों के बारे में पढ़ते समय, मुझे केवल उदाहरण मिलते हैं जब यह शीर्षलेख और पाद लेखों के लिए उपयोग किया जाता है। क्या आपके पास आपके उत्तर के लिए कोई उदाहरण और/या कामकाजी कोड है। –
मैंने संग्रह दृश्य कस्टम लेआउट का उपयोग कर इसे किया है। कस्टम लेआउट में मेरा पूरक दृश्य फ्रेम उस पूरे सेक्शन फ्रेम के बराबर है। – Jirune
आपने इसे कैसे प्रबंधित किया? अगर आप बता सकते हैं कि यह बहुत अच्छा होगा। धन्यवाद –