iOS / Swift:UITableViewCell中的两个嵌入视图在运行时不会出现(iOS/Swift: Two embedded views in UITableViewCell not app

编程入门 行业动态 更新时间:2024-10-04 23:24:36
iOS / Swift:UITableViewCell中的两个嵌入视图在运行时不会出现(iOS/Swift: Two embedded views in UITableViewCell not appearing when run)

我试图在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 methods

Your 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.

更多推荐

本文发布于:2023-08-07 12:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464086.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   两个   Swift   UITableViewCell   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!