2017-12-13 20 views
0

私はしばらくの間問題があり、まだ解決できませんでした。最後に、私は次の問題に関するあなたの助けを求めたかったのです。ネストされたUICollectionView繰り返しセル

プロジェクトでネストされたUICollectionViewを実装しました。メインコレクションビューをスクロールしながら繰り返しセルを表示しています。

この問題を解決する方法を理解できませんでした。

MainPageViewController.swift

import UIKit 

class MainPageViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    print("icinde") 

    let navBar = UINavigationBar() 
    let navItem = UINavigationItem(title: "Promotions") 
    navBar.setItems([navItem], animated: false) 
    self.view.addSubview(navBar) 

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .vertical 
    var collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    collectionView.dataSource = self 
    collectionView.delegate = self 
    collectionView.register(MainWithCarouselCollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
    collectionView.backgroundColor = UIColor.white 
    self.view.addSubview(collectionView) 

    let views = ["navBar":navBar,"collectionView": collectionView] as [String : Any] 

    // 2 
    var allConstraints = [NSLayoutConstraint]() 

    let navBarHeight = NSLayoutConstraint.constraints(
     withVisualFormat: "H:|-0-[navBar]-0-|", 
     options: [], 
     metrics: nil, 
     views: views) 
    allConstraints += navBarHeight 

    let navBarWidth = NSLayoutConstraint.constraints(
     withVisualFormat: "V:|-20-[navBar]", 
     options: [], 
     metrics: nil, 
     views: views) 
    allConstraints += navBarWidth 


    let collectionViewTop = NSLayoutConstraint.constraints(
     withVisualFormat: "V:[navBar]-10-[collectionView]-|", 
     options: [], 
     metrics: nil, 
     views: views) 
    allConstraints += collectionViewTop 


    let collectionViewleftRight = NSLayoutConstraint.constraints(
     withVisualFormat: "H:|-5-[collectionView]-5-|", 
     options: [], 
     metrics: nil, 
     views: views) 
    allConstraints += collectionViewleftRight 

    navBar.translatesAutoresizingMaskIntoConstraints=false 
    collectionView.translatesAutoresizingMaskIntoConstraints=false 

    NSLayoutConstraint.activate(allConstraints) 
    // Do any additional setup after loading the view. 
} 


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

    return 20 
} 

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


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainWithCarouselCollectionViewCell 

    cell.labelicerik = "\(indexPath.row)" 

    print(indexPath.row) 
    return cell 
} 


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width:view.frame.width,height: 150) 
} 
} 

MainWithCarouselCollectionViewCell.swiftあなたはあなたの内側のコレクションビュー、それが表示されるたびにリロードする必要があり

import UIKit 

class MainWithCarouselCollectionViewCell: UICollectionViewCell,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate { 

    private let cellId = "Cell2" 


    var labelicerik:String = { 
     return "" 
    }() 

    let appsCollectionView: UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     layout.scrollDirection = .horizontal 
     return collectionView 
    }() 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     setupViews() 

    } 


    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func setupViews() { 
     backgroundColor = .blue 

     addSubview(appsCollectionView) 

     appsCollectionView.delegate = self 
     appsCollectionView.dataSource = self 


     appsCollectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId) 


     let views = ["appsCollectionView": appsCollectionView] as [String : Any] 

     // 2 
     var allConstraints = [NSLayoutConstraint]() 



     let collectionViewTop = NSLayoutConstraint.constraints(
      withVisualFormat: "H:|-0-[appsCollectionView]-0-|", 
      options: [], 
      metrics: nil, 
      views: views) 
     allConstraints += collectionViewTop 


     let collectionViewleftRight = NSLayoutConstraint.constraints(
      withVisualFormat: "V:|-0-[appsCollectionView]-0-|", 
      options: [], 
      metrics: nil, 
      views: views) 
     allConstraints += collectionViewleftRight 



     appsCollectionView.translatesAutoresizingMaskIntoConstraints=false 
     NSLayoutConstraint.activate(allConstraints) 


    } 



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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! AppCell 

     cell.testlabel.text = "\(labelicerik) + \(indexPath.row) " 


     print(indexPath.row) 
     print(labelicerik) 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
      return CGSize(width:contentView.frame.width/2,height: contentView.frame.height) 
    } 
} 


class AppCell: UICollectionViewCell { 

    let testlabel: UILabel = { 

     let testlabel = UILabel(frame: .zero) 


     return testlabel 
    }() 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setupViews() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func setupViews(){ 
     backgroundColor = .red 
     addSubview(testlabel) 

     testlabel.textAlignment = .center 
     let views = ["testlabel": testlabel] as [String : Any] 

     // 2 
     var allConstraints = [NSLayoutConstraint]() 



     let collectionViewTop = NSLayoutConstraint.constraints(
      withVisualFormat: "H:|-0-[testlabel]-0-|", 
      options: [], 
      metrics: nil, 
      views: views) 
     allConstraints += collectionViewTop 


     let collectionViewleftRight = NSLayoutConstraint.constraints(
      withVisualFormat: "V:|-0-[testlabel]-0-|", 
      options: [], 
      metrics: nil, 
      views: views) 
     allConstraints += collectionViewleftRight 

     testlabel.translatesAutoresizingMaskIntoConstraints=false 
     NSLayoutConstraint.activate(allConstraints) 

    } 
} 
+0

dを使用してcellForItemAtの内部コレクションビューを外部コレクションビューから再ロードする – Vikky

+0

次の同じ問題に関する2番目の問題を追加しました:( –

答えて

0

あなたが旧姓
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainWithCarouselCollectionViewCell 
    cell.labelicerik = "\(indexPath.row)" 
    // you need to call this 
    cell.appsCollectionView.reloadData 
    return cell 
    } 
+0

もう一度いただきありがとうございます。それは私の問題を解決しましたが、それでも似たような問題があります。 –

+0

次の同じ問題に関連する2番目の問題を追加しました:( –

関連する問題