简体   繁体   English

如何在Tableview中使用集合视图显示内容

[英]How to display content using collection view inside a Tableview

Hi All! 大家好!

I am trying to display some content using a collectionView inside a table view, but when I run the application the tableview is still empty and no content is getting display on the collection view. 我试图使用表视图内的collectionView显示一些内容,但是当我运行应用程序时,表视图仍然为空,并且没有任何内容显示在集合视图上。 I have set the datasource and the delegate to the collection view. 我已经将数据源和委托设置为集合视图。 I am wondering how can I fix and what am I doing wrong.Thanks for all replies. 我想知道如何解决以及我在做什么错。感谢所有回复。

表格视图图像

Tableviewcell 
```
class DiscoverTableViewCell: UITableViewCell, UICollectionViewDataSource,UICollectionViewDelegate {


 override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        collectionviewOutlet.delegate = self
        collectionviewOutlet.dataSource = self

        collectionviewOutlet.reloadData()
    }


   func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
//enter image description here
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return businesses.count
    }

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellColl", for: indexPath) as! DiscoverCollectionViewCell
        let business = businesses\[indexPath.row\]
        cell.restaurantName.text = business.name
        cell.restaurantPrice.text = business.price
        return cell
    }
}
```

//Tableview controller 
```
class DiscoverRestaurantTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? DiscoverTableViewCell {
//     

      return cell
        }
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
}
```
//CollectionView cell 
```
class DiscoverCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var restaurantImage: UIImageView!
    @IBOutlet weak var restaurantName: UILabel!

    @IBOutlet weak var restaurantPrice: UILabel!

    @IBOutlet weak var category: UILabel!


}

```

Check if you have set dataSource of tableView like 检查是否已设置tableView的dataSource

tableView.dataSource = self

And try to reload data in tableView like 并尝试像这样重新加载tableView中的数据

tableView.reloadData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Swift在IOS中的应用商店中使用自定义标头操作在Tableview中显示Collection View - How to Display Collection View inside tableview with custom header action like app store in IOS using Swift 如何在tableview单元格内加载内容视图? - How to load content view inside a tableview cell? 如何在 tableview swift 5 内的水平集合视图中显示图像 - How to show images in horizontal collection view inside tableview swift 5 如何在集合视图单元格的某一部分中添加多个数组,该集合视图位于tableView单元格内? - How to add multi arrays in one section of Collection View Cell which that collection view inside tableView cell? 里面有TableView的动态Collection View单元格? - Dynamic Collection View Cell with TableView inside? tableview单元格内的集合视图重新加载数据 - collection view inside tableview cell reload data 如何在单元格内容视图中显示 label? - How to display label inside of a cells Content View? 如何在容器视图中显示WKWebView内容 - How to display WKWebView content inside a container view 在 TableView 控制器中将视图显示为内部应用程序通知 - Display view as inside app notification in TableView Controllers 如何在 tableview header 中收集按钮 - How to have collection of buttons inside tableview header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM