ios - 如何在 Tableview 中使用 Collection View 显示内容

标签 ios swift uitableview

大家好!

我试图在 TableView 中使用 collectionView 显示一些内容,但是当我运行应用程序时, TableView 仍然为空,并且 Collection View 上没有显示任何内容。我已将数据源和委托(delegate)设置为 Collection View 。我想知道我该如何解决以及我做错了什么。感谢所有回复。

Table view image

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!


}

```

最佳答案

检查是否设置了tableView的dataSource,例如

tableView.dataSource = self

并尝试在 tableView 中重新加载数据

tableView.reloadData()

关于ios - 如何在 Tableview 中使用 Collection View 显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56821992/

相关文章:

swift - swift 将 UInt32 拆分为 [UInt8]

SwiftUI:UIViewRepresentable 中的 updateUIView() 函数导致应用程序卡住和 CPU 峰值

iOS:当滚动太快时,UITableView 会混淆数据

ios - 如何从 nib 文件加载 UITableViewCell 高度?

ios - 循环中的 tableView reloadData 不在每次传递时绘制单元格

javascript - Titanium 应用程序可以访问 ios 共享功能吗?

ios - UIStepper 的值(value)

swift - 理解SpriteKit场景坐标

IOS:两个tableview的tableview委托(delegate)方法

iphone - 如何禁用 iPhone 导航栏中的平移手势识别器(如果有)?