2017-04-05 4 views
3

私はUICollectionViewCell内でTableViewを追加しようとしています。そのため、自分でTableViewからそのセルを管理したいと考えています。 indexPath.rowが2の場合は、テーブルビューのセルのcollectionview indexPath.rowを呼び出す必要があります。特定のUICollectionViewCell内部でTableViewをプログラム的に使用しますか?

写真を確認してください、私は赤色で行っています。 UICollectionViewControllerとUICollectionViewControllerFlowLayoutを使用してすべてをプログラムで作成しました。

+0

このスクリーンショットのUICollectionViewはどこにありますか? – luk2302

+0

CollectionViewのように見えない場合でも、画面はすべてUICollectionViewです。 –

答えて

2

あなたはそのindexpathのカスタムUICollectionView細胞を作成することができます。セルxibにテーブルビューを追加します。

カスタムセルクラスでtableviewのデリゲートメソッドを実装します。それを必要とする人のために

class CustomCollectionViewCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate 
    { 
    @IBOutlet var tableView: UITableView! 

    override func layoutSubviews() 
    { 
     super.layoutSubviews() 
     tableView.delegate = self 
     tableView.dataSource = self 
    } 
    } 
2

、私は解決策が見つかりました:

collectionView?.register(CustomizedCell.self, forCellWithReuseIdentifier: "cell") 

その後:私はこれをしなかったCollectionViewでviewDidLoadメソッドで、次に

class CustomizedCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate { 

    var tableView = UITableView() 
    let cellIdentifier: String = "tableCell" 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     tableView.delegate = self 
     tableView.dataSource = self 

     tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier) 
} 
func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 4 
} 

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

    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: cellIdentifier) 

    cell.textLabel?.text = "1 CUP" 
    cell.detailTextLabel?.text = "Whole" 

    return cell 
} 

}

を私はUICollectionViewのcellForRowAt indexPathメソッドでこのように呼びました:

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

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

    return cell 
} 
+0

'UITableview'をコレクションセルに追加すると表示されませんか? – skymook