2017-08-01 5 views
2

2つのコレクションビューを同じビューに組み込むことを試みています。私はすべてを実装し、2番目のコレクションビューに読み込んでいるデータが1番目のコレクションビューと同じであることに気付きました。ここで複数のコレクションビューをビューに追加する方法

は私がプロジェクトを実行すると、両方のコレクションビューは、Appleの絵を見せているし、細胞数が10

ある

class DashBoardMainSection1CollectionViewCellClass: UICollectionViewCell 
{ 

    @IBOutlet weak var DashBoardMainSection1CollectionViewImageListingViewOutlet : UIImageView! 

    @IBOutlet weak var DashBoardMainSection1CollectionViewLabelOutlet    : UILabel! 

} 

class DashBoardMainSection2CollectionViewCellClass: UICollectionViewCell 
{ 

    @IBOutlet weak var DashBoardMainSection2CollectionViewImageListingViewOutlet : UIImageView! 

    @IBOutlet weak var DashBoardMainSection2CollectionViewLabelOutlet    : UILabel! 

} 


private let reuseIdentifierDashBoardMainSection1Cell = "dashBoardMainSection1CollectionViewCellIdentifier" 

private let reuseIdentifierDashBoardMainSection2Cell = "dashBoardMainSection2CollectionViewCellIdentifier" 

class DashBoardViewControllerMain: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout 
{ 



    @IBOutlet weak var DashBoardMainSection1CollectionViewOutlet : UICollectionView! 

    @IBOutlet weak var DashBoardMainSection2CollectionViewOutlet : UICollectionView! 


    override func viewDidLoad() 
    { 
     super.viewDidLoad() 



     DashBoardMainSection1CollectionViewOutlet.delegate = self; 
     DashBoardMainSection2CollectionViewOutlet.delegate = self; 
     DashBoardMainSection1CollectionViewOutlet.dataSource = self; 
     DashBoardMainSection2CollectionViewOutlet.dataSource = self; 

     // Do any additional setup after loading the view. 
    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of items 
     var count:Int? 
     if (self.DashBoardMainSection1CollectionViewOutlet) != nil{ 
      count = 10 
     } 
     //if (self.DashBoardMainSection2CollectionViewOutlet) != nil{ 
     else{ 
      count = 5 
     } 

     return count! 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if let collectionView = self.DashBoardMainSection1CollectionViewOutlet{ 

      let cell:DashBoardMainSection1CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection1Cell, for: indexPath) as! DashBoardMainSection1CollectionViewCellClass 


      cell.DashBoardMainSection1CollectionViewImageListingViewOutlet.image = UIImage(named:"Apple.png") 

      cell.DashBoardMainSection1CollectionViewLabelOutlet.text = "FIRST" 

      return cell 



     } 
     else{  
      let cell2:DashBoardMainSection2CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection2Cell, for: indexPath) as! DashBoardMainSection2CollectionViewCellClass 


      cell2.DashBoardMainSection2CollectionViewImageListingViewOutlet.image = UIImage(named:"Logo-Disabled.png") 

      cell2.DashBoardMainSection2CollectionViewLabelOutlet.text = "SECOND" 

      return cell2 

     } 
    } 

をしようとしていますコードは、誰かが解決するために私を助けることができていますこの問題...

答えて

0

条件if (self.DashBoardMainSection1CollectionViewOutlet) != nilは常にtrueを返し、ifのコードは毎回実行されるだけなので、

このお試しください:問題は、if文である

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of items 
     var count = 0 
     if (collectionView == self.DashBoardMainSection1CollectionViewOutlet) { 
      count = 10 
     } 
     // if (collectionView == self.DashBoardMainSection2CollectionViewOutlet) 
     else { 
      count = 5 
     } 

     return count 
    } 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if (collectionView == self.DashBoardMainSection1CollectionViewOutlet){ 

      let cell:DashBoardMainSection1CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection1Cell, for: indexPath) as! DashBoardMainSection1CollectionViewCellClass 
      cell.DashBoardMainSection1CollectionViewImageListingViewOutlet.image = UIImage(named:"Apple.png") 

      cell.DashBoardMainSection1CollectionViewLabelOutlet.text = "FIRST" 
      return cell 

     } 
     else{  
      let cell2:DashBoardMainSection2CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection2Cell, for: indexPath) as! DashBoardMainSection2CollectionViewCellClass 
       cell2.DashBoardMainSection2CollectionViewImageListingViewOutlet.image = UIImage(named:"Logo-Disabled.png") 

      cell2.DashBoardMainSection2CollectionViewLabelOutlet.text = "SECOND" 
      return cell2 

     } 
} 
0

UICollectionViewのデータが同じ場合は、同じDataSourceを使用していることを意味します。 それぞれに対してDataSourceを定義してくださいUICollectionView また、両方に対して同じアクションを実装する必要がある場合を除き、委任者に対しても同じ操作を行う必要があります。

1

を:

それは次のようにする必要があります:collectionViewがで渡されたかどうかをチェック、このコードでは

if collectionView == self.DashBoardMainSection1CollectionViewOutlet{ 
    //code 
} 

パラメータは最初のcollectionViewです。

元のコードでは、collectionViewという名前の新しい定数を作成し、最初にcollectionViewを割り当てました。

1

collectionviewタグを両方とも管理してください。 viewDidLoadメソッドのcollectionviewまたはストーリーボードの両方にタグを付けます。今度はタグに基づいて条件を作ります。

例: -

DashBoardMainSection1CollectionViewOutlet.tag = 1 
DashBoardMainSection2CollectionViewOutlet.tag = 2 

if collectionView.tag == 1 { 
    return 10 
} else { 
    return 5 
} 
+0

'あなたのブロックは常にtrue – Krunal

+1

を返しますので、もしDashBoardMainSection1CollectionViewOutlet.tag'は常に、1のままになります'場合collectionView.tagの== 1 ' – Krunal

+0

であなたの答えを更新はい、あなたは正しいです。 @Krunal。 – shahnilay86

関連する問題