gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:38:13 25 4
gpt4 key购买 nike

大家好!

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com