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

时间:2019-02-21 20:59:18

标签: swift uitableview uicollectionview

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

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。

结论我需要在表视图的第一个单元格中有一个集合视图,而其他单元格应仅包含文本。

1 个答案:

答案 0 :(得分:1)

如果您将表格视图设置为两个部分,将会更加容易。第一部分对于具有集合视图的单元格将有一行。第二部分将包含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
}
相关问题