Programim dhe zhvillim, javascript, python, php, html

Si të hyni në një koleksionView që është i ngulitur në një koleksion prindViewCell në UIViewController kryesor?

Si të hyni në një koleksionView që është i ngulitur në një koleksion prindViewCell në UIViewController kryesor?

ViewController im kryesor është një UICollectionView i cili strehon një UICollectionViewCell të quajtur BannerCell. BannerCell ka një koleksionView i cili përmban 3 qeliza:

  1. Qeliza e titujve
  2. Qeliza e imazhit
  3. Postimet Cell

Brenda PostsCell, ekziston një koleksionPamje qelizash që përmban postime.

fut përshkrimin e imazhit këtu

Unë dua të aksesoj pamjen e koleksionit brenda postCell, por nuk mund të marr referencën e këtij koleksioniView përmes një delegati.

import UIKit

protocol HomeBannerPostsCellDelegate: class {
  func homeBannerPostsCellDelegateMethod1(enabled: Bool)
}

class HomeBannerHeaderPostsCollectionCell: UICollectionViewCell {
   @IBOutlet weak var bannerPostsCollectionView: UICollectionView!
   var delegate: HomeBannerPostsCellDelegate?
}

extension HomeBannerHeaderPostsCollectionCell : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeBannerHeaderPostCollectionCell", for: indexPath) as! HomeBannerHeaderPostCollectionCell
      cell.backgroundColor = UIColor.red

      if(indexPath.item==1 || indexPath.item==3 ){
          cell.backgroundColor = UIColor.blue
          return cell
      }
      return cell
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let screenWidth = UIScreen.main.bounds.width
    let frameHeight = screenWidth*0.10
    return CGSize(width: screenWidth*0.75, height: frameHeight)
  }
}

Unë tashmë mund të marr HomeBannerPostsCellDelegate për të ekzekutuar metodën që printon një rresht në kontrolluesin kryesor.

   if(bannerCellReuseIdentifier == widgets[indexPath.item]){
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: bannerCellReuseIdentifier, for: indexPath) as! HomeBannerCollectionCell
        cell.backgroundColor = UIColor.lightGray
        cell.delegate=self
        cell.bannerPostsDelegate=self
        self.bannerPostsCollectionView=cell.bannerPostsCollectionView
        return cell
    }

Në metodën ViewDidLoad().

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

        // This view controller itself will provide the delegate methods and row data for the table view.
    collectionView.delegate = self
    collectionView.dataSource = self

    // Register XIB for Supplementary View Reuse
    let XIB = UINib.init(nibName: "SectionHeader", bundle: Bundle.main)
    collectionView.register(XIB, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HeaderIdentifier)

    let layout = StickyHeadersCollectionViewFlowLayout()
    collectionView.setCollectionViewLayout(layout, animated: true)
    collectionView.backgroundColor = UIColor.white

    collectionView.contentInset = UIEdgeInsets(top: -collectionView.bounds.width * 0.20 * 2, left: 0, bottom: 0, right: 0)

    self.homeBannerPostsCellDelegateMethod1(enabled: true)

    setTimer()

}

Po përpiqem të bëj që PostsCollectionView të rrotullohet duke përdorur setTimerMethod

 func setTimer() {
    let _ = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(autoChangeBanner), userInfo: nil, repeats: true)
}

var bannerPostIndex = 0
@objc func autoChangeBanner() {
    NSLog("autoChangeBanner")
    let indexPath = IndexPath(item: bannerPostIndex, section: 0)
    self.bannerPostsCollectionView?.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
    bannerPostIndex=bannerPostIndex+1
    if(bannerPostIndex>3){
        bannerPostIndex=0
    }
}

Por bannerPostsCollectionView është zero.

A ka ndonjë mënyrë që mund ta dërgoj referencën e bannerPostsCollectionView në MainController?

Faleminderit paraprakisht.


Përgjigjet:


1

Në kontrolluesin kryesor në qelizë zgjodhi metodën e deligimit, do të kontrolloni nëse qeliza e prekur është qeliza me banderolë.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)

        if let bannerCell = cell as? HomeBannerHeaderPostsCollectionCell {
              bannerCell.bannerPostsCollectionView ....
              return
          }

        //And here you do whatever you want for the other cells
    }

Nëse po, ju do ta transferoni qelizën si një HomeBannerHeaderPostsCollectionCell dhe prej saj do të hyni në variablin e saj cell.bannerPostsCollectionView dhe mund të bëni atë që dëshironi

02.03.2018
Materiale të reja

Masterclass Coroutines: Kapitulli-3: Anulimi i korutinave dhe trajtimi i përjashtimeve.
Mirë se vini në udhëzuesin gjithëpërfshirës mbi Kotlin Coroutines! Në këtë seri artikujsh, unë do t'ju çoj në një udhëtim magjepsës, duke filluar nga bazat dhe gradualisht duke u thelluar në..

Faketojeni derisa ta arrini me të dhënat false
A e gjeni ndonjëherë veten duke ndërtuar një aplikacion të ri dhe keni nevojë për të dhëna testimi që duken dhe duken më realiste ose një grup i madh të dhënash për performancën e ngarkesës...

Si të përdorni kërkesën API në Python
Kërkesë API në GitHub për të marrë depot e përdoruesve duke përdorur Python. Në këtë artikull, unë shpjegoj procesin hap pas hapi për të trajtuar një kërkesë API për të marrë të dhëna nga..

Një udhëzues hap pas hapi për të zotëruar React
Në këtë artikull, do të mësoni se si të krijoni aplikacionin React, do të mësoni se si funksionon React dhe konceptet thelbësore që duhet të dini për të ndërtuar aplikacione React. Learning..

AI dhe Psikologjia — Pjesa 2
Në pjesën 2 të serisë sonë të AI dhe Psikologji ne diskutojmë se si makineritë mbledhin dhe përpunojnë të dhëna për të mësuar emocione dhe ndjenja të ndryshme në mendjen e njeriut, duke ndihmuar..

Esencialet e punës ditore të kodit tim VS
Shtesat e mia të preferuara - Git Graph 💹 Kjo shtesë është vërtet e mahnitshme, e përdor përpara se të filloj të punoj për të kontrolluar dy herë ndryshimet dhe degët më të fundit, mund të..

Pse Python? Zbulimi i fuqisë së gjithanshme të një gjiganti programues
Në peizazhin gjithnjë në zhvillim të gjuhëve të programimit, Python është shfaqur si një forcë dominuese. Rritja e tij meteorike nuk është rastësi. Joshja e Python qëndron në thjeshtësinë,..