2017-02-16 117 views
0

我在滾動視圖中有多個集合視圖。我有一個數據顯示不正確的問題(第一頁上的項目數量與最後一個項目的數量相同)。我想這個問題是在收集視圖的數據源對象。我讀到:Swift:如何在一個視圖控制器中爲多個集合視圖初始化多個數據源

collectionView.dataSource = MyDataSource() 是錯誤的,因爲dataSource是一個弱引用,因此需要通過強引用將其存儲,以便在創建它之後保持活動狀態。在ViewController中添加了一個私有屬性,以保持強引用,初始化,然後分配它修復問題

但我不明白我怎麼能爲多個數據集呢?

pages = musicItems.count/6+1 //numberOfItems%itemsOnTheScreen 
    self.scrollView.delegate = self; 
    self.scrollView.isScrollEnabled = true 
    self.scrollView.isPagingEnabled = true 
    bounds = self.scrollView.bounds 

    let flowLayout = CVLayout() 
    for i in 0...pages-1 { 
     let x = bounds.width*CGFloat(i) 
     let y = bounds.origin.y 
     let width = bounds.width 
     let height = bounds.height 
     let collectionViewElement = UICollectionView(frame: CGRect(x:x,y:y,width:width,height:height), collectionViewLayout: flowLayout) 
     if let page = i as? Int { 
      collectionViewElement.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: "cell\(page)")} 

     var ds: CVDataSource = CVDataSource() 
     ds.page = i 
     ds.itemsCount = itemsOnThePage(currentPage: i) 
     collectionViewElement.delegate = ds//self 
     collectionViewElement.dataSource = ds//self 
     print("set value \(i)") 

     collectionViewElement.isScrollEnabled = false 
     collectionViewElement.backgroundColor = UIColor.white 
     self.scrollView.addSubview(collectionViewElement) 

    } 
+0

埃琳娜請上傳任何屏幕截圖,以更好地瞭解您的要求。 – iDeveloper

回答

0

添加的陣列到視圖控制器var dataSources: CVDataSource = [],然後在循環內添加上述數據源dataSources.append(ds)。你的VC將有一個強大的參考數組,這將有一個強大的參考每個數據源。

相關問題