gpt4 book ai didi

swift - 两个不同单元格的 TableView 相同的 cellForRowAt 用法

转载 作者:行者123 更新时间:2023-11-28 05:45:49 29 4
gpt4 key购买 nike

我有 TableView (不是 TableView Controller )。表格 View 的一个单元格是 Collection View ,其他单元格只是文本标签。这是我的代码

import UIKit

class MainViewController: UIViewController, UITableViewDataSource,UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!

var imageNames = [ImageNames]()
var foodNames = [FoodNames]()

override func viewDidLoad() {
super.viewDidLoad()

imageNames = [
ImageNames(name: "images"),
ImageNames(name: "unnamed"),
ImageNames(name: "unnamed"),
ImageNames(name: "images"),
ImageNames(name: "images")
]

foodNames = [
FoodNames(title: "Hamburger hamburger big king big mac big chicken")
]

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = true
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
searchBar.text = ""
searchBar.resignFirstResponder()
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 130
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageNames.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainFoodTableViewCell", for: indexPath) as? MainFoodTableViewCell

cell?.mainFoodCollectionView.delegate = self
cell?.mainFoodCollectionView.dataSource = self
cell?.mainFoodCollectionView.reloadData()
cell?.mainFoodCollectionView.tag = indexPath.row

return cell!

}


//collection view cell size

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: ((self.view.frame.size.width / 2) + 16), height: 130)
}

//collection view cell data


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainFoodCollectionViewCell", for: indexPath) as! MainFoodCollectionViewCell
let img = imageNames[indexPath.row]
cell.mainFoodImage.image = UIImage(named: img.name)
return cell
}
}

如何在同一个 cellForRowAt 方法中使用下面的代码。

    let food: FoodNames
food = foodNames[indexPath.row]
cell?.textLabel!.text = food.title

我尝试为其他表格 View 单元格中的文本数据创建其他 ViewController。

结论 我需要在 TableView 的第一个单元格中有一个 Collection View ,而其他单元格应该只有文本。

最佳答案

如果你用两个部分设置你的 TableView 会容易得多。第一部分将有一行用于包含 Collection View 的单元格。第二部分将包含 foodNames 数组元素的行。

func numberOfSections(in tableView: UITableView) -> Int {
return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? 1 : foodNames.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainFoodTableViewCell", for: indexPath) as! MainFoodTableViewCell

cell.mainFoodCollectionView.delegate = self
cell.mainFoodCollectionView.dataSource = self
cell.mainFoodCollectionView.reloadData()
cell.mainFoodCollectionView.tag = indexPath.row

return cell
} else {
// Use your actual id and cell class name
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodNameCell", for: indexPath) as! FoodNameCell
cell.textLabel?.text = foodNames[indexPath.row].title

return cell
}
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.section == 0 ? 130 : 44
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.section == 0 ? 100 : 44
}

关于swift - 两个不同单元格的 TableView 相同的 cellForRowAt 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54816131/

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