2012-11-28 17 views
24

UICollectionView में, मैं एक एकल सेल या पूरे संग्रह दृश्य के बजाय, पूरे अनुभाग को एक समान पृष्ठभूमि रंग देना चाहता हूं।UICollectionView में पूरे अनुभाग का पृष्ठभूमि रंग कैसे बदलें?

मुझे ऐसा करने के लिए कोई प्रतिनिधि तरीका नहीं दिख रहा है, कोई सुझाव?

उत्तर

2

मैंने अभी तक यह कोशिश नहीं की है, लेकिन मुझे लगता है कि अगर आप अपनी कोशिकाओं के पीछे पृष्ठभूमि चाहते हैं (पुस्तकें ऐप में शेल्फ की तरह) तो आपको सजावट के दृश्यों का उपयोग करने की आवश्यकता है। मुझे लगता है कि आपको प्रत्येक सेक्शन के लिए अलग-अलग विचार प्राप्त करने में सक्षम होना चाहिए, और उन्हें प्रतिनिधि विधि layoutAttributesForDecorationViewOfKind:atIndexPath: का उपयोग करके सेट अप करना चाहिए।

0

संग्रह दृश्य में प्रत्येक अनुभाग में पूरक विचार हो सकते हैं, इसलिए प्रत्येक सेक्शन के लिए पूरक दृश्य रखें, फिर अनुभाग कक्षों के बजाय पूरक दृश्यों में पृष्ठभूमि रंग सेट करें। मुझे उम्मीद है इससे मदद मिलेगी।

+0

पूरक विचारों के बारे में पढ़ते समय, मुझे केवल उदाहरण मिलते हैं जब यह शीर्षलेख और पाद लेखों के लिए उपयोग किया जाता है। क्या आपके पास आपके उत्तर के लिए कोई उदाहरण और/या कामकाजी कोड है। –

+0

मैंने संग्रह दृश्य कस्टम लेआउट का उपयोग कर इसे किया है। कस्टम लेआउट में मेरा पूरक दृश्य फ्रेम उस पूरे सेक्शन फ्रेम के बराबर है। – Jirune

+1

आपने इसे कैसे प्रबंधित किया? अगर आप बता सकते हैं कि यह बहुत अच्छा होगा। धन्यवाद –

2

यह UICollectionView खंड रंग बदलने के लिए एक महान ट्यूटोरियल है:

http://www.ericjchapman.com/jekyll/update/ios/2014/11/10/ios-changing-section-background-color-in-UICollectionView.html

मूल रूप से उपयोगकर्ता @casamia को

अपडेट किया गया लिंक, धन्यवाद, हम क्रम में UICollectionViewLayoutAttributes, UICollectionReusableView और UICollectionViewLayout उपवर्ग करना होगा अनुभाग के पृष्ठभूमि दृश्य के रूप में एक उदाहरण UICollectionReusableView बनाने के लिए।

enter image description here

अधिक जानकारी के विवरण के लिए लिंक का अनुसरण करें:

यह अपने परिणाम है।

+0

आपके द्वारा लिंक किया गया लिंक बदल गया है। http://www.ericjchapman.com/jekyll/update/ios/2014/11/10/ios-changing-section-background-color-in-UICollectionView.html – casamia

+3

और अब @ कसमिया द्वारा प्रदान किया गया लिंक भी मर चुका है। यहां कम से कम जिथब प्रोजेक्ट का एक लिंक है: https://github.com/ericchapman/ios_decoration_view – TylerJames

-2

मैंने प्रत्येक विधि के पृष्ठभूमि रंग को निम्न विधि में एक बहुत ही सरल तरीके से बदल दिया है: लेकिन मुझे यकीन नहीं था कि यह करना सही है या नहीं। लेकिन यह काम किया।

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    FamilyCalendarCellItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"calendarItem" forIndexPath:indexPath]; 
    Event *event; 
    _headerView = [collectionView dequeueReusableSupplementaryViewOfKind: 
        UICollectionElementKindSectionHeader withReuseIdentifier:@"EventHeader" forIndexPath:indexPath]; //headerView is declared as property of Collection Reusable View class 
    if(indexPath.section==0) { 

     cell.backgroundColor=[UIColor orangeColor]; 
     } 
    else if(indexPath.section==1) { 

     cell.backgroundColor=[UIColor yellowColor]; 

    } 

    return cell; 
} 
+0

यह केवल कोशिकाओं के पृष्ठभूमि रंग को बदलता है। प्रश्न यह है कि किसी दिए गए खंड में संग्रह दृश्य के पृष्ठभूमि रंग को कैसे बदला जाए – TylerJames

0

विचार एक रंग विशेषता जोड़ने के लिए UICollectionViewLayoutAttributes ओवरराइड करने के लिए है। और उसके बाद ओवरराइड करें UICollectionReusableView दृश्य पृष्ठभूमि में रंग लागू करें।

https://github.com/strawberrycode/SCSectionBackground

0

मैं यहाँ 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 में ऊंचाई के साथ गड़बड़ कर दिया। आपको इसे अपनी परियोजना के लिए बदलना चाहिए।

संबंधित मुद्दे