我试图在UITableViewCell嵌入一个UIStackView 。 UIStackView内部应该是两个项目: UICollectionView和另一个UITableView 。
我采取的方法是将所有内容嵌入到根UIViewController包含的UIView中。
我从父UITableView创建了一个dataSource出口,并使根UIViewController符合UITableViewDataSource 。 之后,我实现了标准的UITableView函数,并为UITableView中的嵌入式UICollectionView做了类似的事情。 以下是根UIViewController和自定义UITableViewCell的代码:
class OverviewController: UIViewController, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "AppTitle" } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "appCell", for: indexPath) as! AppCell return cell } } class AppCell: UITableViewCell, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "featureCell", for: indexPath) as! AppCollectionCell return cell } }当我运行它时,我只得到一个空表视图:
我究竟做错了什么?
am trying to embed a UIStackView inside of a UITableViewCell. Inside of the UIStackView should be two items: a UICollectionView and another UITableView.
The approach I am taking is to embed everything inside of a UIView contained in a root UIViewController.
I have created a dataSource outlet from the parent UITableView and made the root UIViewController conform to UITableViewDataSource. Afterwards, I implemented the standard UITableView functions and did a similar thing for the embedded UICollectionView in the UITableViewCell. Here is the code for the root UIViewController and custom UITableViewCell:
class OverviewController: UIViewController, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "AppTitle" } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "appCell", for: indexPath) as! AppCell return cell } } class AppCell: UITableViewCell, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "featureCell", for: indexPath) as! AppCollectionCell return cell } }When I run this, I just get an empty table view:
What am I doing wrong?
最满意答案
我为你做了一个小项目: https : //github.com/mvdizel/q44833725
使用与项目中相同的dataSource引用 小心实现数据源方法您的top table view包含数据源 - 您的视图控制器。 其他一个表视图(在顶部表视图单元格内)和集合视图都有数据源 - 您的单元格。
I made a little project for you: https://github.com/mvdizel/q44833725
Use same dataSource's references as in project Be careful with implementing data source methodsYour top table view has data source - your view controller. Other one table view (inside the top table view cell) and collection view, both has data source - your cell.
更多推荐
发布评论