【问题标题】:Table view same cellForRowAt usage for two different cell表查看两个不同单元格的相同 cellForRowAt 用法
【发布时间】:2019-02-21 20:59:18
【问题描述】:

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

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。

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

【问题讨论】:

    标签: swift uitableview uicollectionview


    【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多