2017-01-03 18 views
0

私はUITableViewを持っていて、その中に2つありますUICollectionViewsです。UICollectionViewのコンテンツサイズに応じてUITableViewCellの高さを設定する方法は?

私の必要条件は、最初にスクロールする必要があることです。UICollectionViewは水平方向です。 そして私はスクロールする必要があるのでUITableViewを垂直にスクロールする必要はありません。UICollectionViewをスクロールします。あなたがその最初の(ヘッダー「フォルダ」)を見ることができ、画像内

UICollectionView私は水平方向と第二(ヘッダ「製品」)をスクロールする必要がUICollectionView私はUITableViewコンプリートをスクロールする必要があるため、私は垂直方向にスクロールする必要はありません。

collectionViewはいずれもTableViewCellであり、第2のUICollectionViewのスクロールを無効にしています。

商品を追加して削除する必要がありますが、どのようにしてTableViewSecondセルの高さを設定する必要がありますか?ここで

enter image description here

enter image description here

私のコードです:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if (0 == indexPath.section) { 
     var addFolderCell = tableView.dequeueReusableCell(withIdentifier: "AddFolderCell") as? AddFolderCell 

     if(nil == addFolderCell) { 
      let nib:Array = Bundle.main.loadNibNamed("AddFolderCell", owner: self, options: nil)! 
      addFolderCell = nib[0] as? AddFolderCell 
     } 

     addFolderCell?.collectionView.delegate = self 
     addFolderCell?.collectionView.dataSource = self 

     return addFolderCell! 
    } else { 
     let identifier = "CreateFolderCell" 
     var createFolderCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CreateFolderCell 

     if(createFolderCell == nil) { 
      let nib:Array = Bundle.main.loadNibNamed("CreateFolderCell", owner: self, options: nil)! 
      createFolderCell = nib[0] as? CreateFolderCell 
     } 

     return createFolderCell! 
    } 
} 

//Height for row. 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    //let index : NSIndexPath = NSIndexPath(row: 0, section: 1) 
    if 0 == indexPath.section { 
     return 135 
    } 
    return 300 
} 

//ヘッダの高さ。

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { 
    return 20 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return sectionsArray[section] as? String 
} 
} 

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

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

    //cell.backgroundColor = UIColor.white 
    cell.layer.borderWidth = 0.4 
    cell.layer.borderColor = UIColor.lightGray.cgColor 
    cell.layer.cornerRadius = 2 
    //CreateCardView(viewCorner: cell.cardView) //cell.cardView 
    return cell 
    } 
} 

// MARK:コレクションビューレイアウト

extension GroupDataView : UICollectionViewDelegateFlowLayout { 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: 90, height: 130) 
    } 
} 

私は何ができますか?どのようにtableViewの高さを設定できますか?

+0

tableView.rowHeightを?ストーリーボードに定義されていますか? – OhadM

+0

はい、2番目のコレクションビューのコンテンツサイズに従って2番目のセルの高さが設定されるため、どのように設定できますか。 ありがとう – kishor0011

+0

ViewLayoutメソッドをオーバーライドすると作業が正常に進んでいるようです。 コレクションビューのセルの高さをテーブルビューのセルの高さに渡すのは問題ですか? – OhadM

答えて

1

は、私はあなたには、いくつかのいくつかの手順を使用して、これを実現することができると思う:

  1. サブクラスはあなたのUITableViewCell、のはそれCollectionCellに名前を付け、新しいサブクラス名にIBインターフェースで、それのクラスを変更することができます。

  2. UICollectionViewからCollectionCellまでの出口を作成します。ここで、UICollectionViewが配置されます。それをcollectionViewと名をつけましょう。

  3. セル境界にcollectionViewのすべての辺に制約を追加します。 (上、 右、下、左)。

  4. collectionViewのためにIBで追加し、CollectionCellへのコンセントを作成してください。それをcollectionViewHeightと名をつけましょう。 tableView:cellForRowAtIndexPath:

  5. コードを追加します

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        // ... 
        // normal cell declarations you wish 
        // ... 
    
        (cell as! CollectionCell).collectionView.reloadData() 
        let height = cell.collectionView.collectionViewLayout.collectionViewContentSize.height 
        (cell as! CollectionCell).collectionViewHeight.constant = height 
        return cell 
    } 
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
        return 100 // <- Your Desired Height 
    } 
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
        return UITableViewAutomaticDimension 
    } 
    
関連する問題