[英]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.