Swift 表视图相同单元格FORROWAT两个不同单元格的用法

Swift 表视图相同单元格FORROWAT两个不同单元格的用法,swift,uitableview,uicollectionview,Swift,Uitableview,Uicollectionview,我有表视图(不是表视图控制器)。表视图的一个单元格是集合视图,其他单元格仅为文本标签。这是我的密码 import UIKit class MainViewController: UIViewController, UITableViewDataSource,UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UISearchBarDelegate { @IBOutlet weak var

我有表视图(不是表视图控制器)。表视图的一个单元格是集合视图,其他单元格仅为文本标签。这是我的密码

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
我尝试为其他表视图单元格中的文本数据创建其他ViewController


结论我需要表视图的第一个单元格中的一个集合视图,其他单元格应该只有文本

如果您用两个部分设置表视图,会容易得多。第一部分将有一行用于具有集合视图的单元格。第二部分将包含
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
}