ios - every 4 block another background color in a uicollectionView -


मेरे पास collectionView cells की एक सूची है। प्रत्येक सेल में एक पृष्ठभूमि रंग है। तो यह ऐसा होना चाहिए।

  • सेल 1 -> लाल रंग
  • सेल 2 -> ग्रीन रंग
  • सेल 3 -> ऑरेंज रंग
  • सेल 4 -> पीला रंग
  • सेल 5 -> लाल रंग
  • सेल 6 -> ग्रीन रंग
  • सेल 7 -> ऑरेंज रंग
  • सेल 8 -> पीला रंग
  • सेल 9 -> लाल रंग
  • सेल 10 -> ग्रीन रंग
  • सेल 11 -> ऑरेंज रंग
  • सेल 12 -> पीला रंग

फिलहाल मैं इसे इस तरह से कर रहा हूँ। मैंने निजी int मेरे ViewController में और मेरे cellForItemAtIndexPath में घोषित किया है, मैं यह करता हूं।

  - (UICollectionViewCell *) संग्रह देखें: (UICollectionView *) सीवी सेलफोरआईटीएमएटीआईएंडएक्सपैथ: (एनएसआईएनएक्सएपैथ *) इंडेक्सपेथ {// मैगज़ीनक्ल्यूविक्यूज सेल * सेल = (मैगज़ीनकॉल्यूशन दृश्यकेल *) [सीवी डेक्यूयूआरयूईबलकेलवाइट रीयूइजेंटिफ़ायर: इंडीएक्सपाथ के लिए @ "मैगज़ीन कॉलप्लेक्यूजसेल" indexPath]; ReisCategorieCell * सेल = [cv dequeueReusableCellWithReuseIdentifier: @ "ReisCategorieCell" forIndexPath: indexPath]; Cell.delegationListener = self; रीस्क्रक्चुरीज * श्रेणी = [एआरएसीएचर्स ऑब्जेक्टऑटएंडएक्स: इंडेक्सपाथ.रो]; Cell.reisCategorie = श्रेणी; Cell.lblTitle.font = [UIFont fontWithName: @ "Dosis-Medium" आकार: 35]; Cell.lblDescription.font = [UIFont fontWithName: @ "Dosis-Medium" आकार: 15]; [Cell.btnAanbod.titleLabel सेटफॉन्ट: [UIFont fontWithName: @ "Dosis-Medium" आकार: 23]]; Cell.lblTitle.textColor = [UIColor whiteColor]; Cell.lblDescription.textColor = [UIColor whiteColor]; [सेल.बीटीएनएएनबोड .टाइटलेबल सेटटैक्स्टकोर: [UIColor whiteColor]]; Cell.btnAanbod.titleLabel.textColor = [UIColor whiteColor]; countBackground ++; यदि (countBackground & gt; 4) {countBackground = 0; } यदि (countBackground == 1) {cell.contentView.backgroundColor = [UIColor aslRed]; Cell.btnAanbod.backgroundColor = [UIColor aslBrown]; } और अगर (countBackground == 2) {cell.contentView.backgroundColor = [UIColor aslBrown]; Cell.btnAanbod.backgroundColor = [UIColor aslGray2]; } और अगर (गणना पृष्ठभूमि == 3) {cell.contentView.backgroundColor = [UIColor aslGray]; Cell.btnAanbod.backgroundColor = [UIColor aslBrown]; } और अगर (countBackground == 4) {cell.contentView.backgroundColor = [UIColor aslGray2]; Cell.btnAanbod.backgroundColor = [UIColor aslBrown]; } अगर ([category.cat_name isqualToString: @ "लैपलैंड"]) {सेल.बीटीएनएलैपलैंड। Hidden = NO; } Else {cell.btnLapland.hidden = हाँ; } [Cell.btnAanbod सेटटाइटल: एनएसएलोकलाइज्ड स्ट्रिंग (@ "बीटीएनएएनबोड", शून्य) के लिए स्टेट: यूआईसीओन्ट्रॉलस्टेटअनैरल]; Cell.lblTitle.text = category.cat_name; Cell.lblDescription.text = category.cat_description; रिटर्न सेल; }  

यह काम करता है अगर आप धीरे धीरे स्क्रॉल करते हैं लेकिन अगर आप तेजी से स्क्रॉल कर रहे हैं तो रंगों को मिश्रित किया जा रहा है तो क्या कोई मुझे इसके साथ मदद कर सकता है?

आपको indexPath.row मैन्युअल सेल गिनती का उपयोग करने के बजाय यह काम नहीं करता क्योंकि आप अपने UICollectionView को स्क्रॉल करते समय अपनी कोशिकाओं का पुन: उपयोग करते हैं

यह मानते हुए कि आपके पास अपने UICollectionView में एक खंड है, तो आप प्रत्येक सेल के लिए रंग निर्धारित करने के लिए ऐसा कुछ इस्तेमाल कर सकते हैं:

  स्विच (indexPath .row% 4) {मामला 0: // पीला विराम; मामला 1: // ऑरेंज ब्रेक; मामले 2: // हरी तोड़; मामले 3: // लाल तोड़; }  

मैंने इस तर्क का परीक्षण नहीं किया है, लेकिन यह सामान्य दृष्टिकोण कार्य करना चाहिए।


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -