何将TableView放入CollectionViewCell中

何将TableView放入CollectionViewCell中

本文介绍了如何将TableView放入CollectionViewCell中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个单元格的tableView(由MVVM体系结构创建).

I have a tableView with several cells (created by MVVM-architecture).

在ViewController中,我像这样填充tableView:

In ViewController I fill my tableView like this:

tbv.registerCells(
        withModels:
        FirstViewModel.self,
        SecondViewModel.self,
        ThirdViewModel.self)

我的任务是将TableView放入CollectionView的一个单元格中.我以为我必须先在ViewController中创建CollectionView,然后再创建CollectionViewCell和CollectionCellViewModel,但是我不知道该怎么做.

My task is to put my TableView in one cell of CollectionView. I thought l have to create CollectionView in my ViewController, after it create CollectionViewCell and CollectionCellViewModel, but how to do it exactly I don't understand.

如果您知道如何实现,请提供帮助.

If you know, how to make it, help.

推荐答案

如何在我的一个应用程序的集合视图中具有多个表视图.首先,我有一个视图控制器,用于在其中构建集合视图.正如新设计指南中通常所建议的那样,我在此视图控制器的扩展中具有集合视图"委托和数据源.

How I have several tableviews in collection views in one of my apps. First I have a view controller in which I build my collection view. As usually proposed in the new design guidelines, I have the Collection View delegate and data source in an extension of this view controller.

在视图控制器中,为表视图定义一个委托和数据源.优选地,这是不同的类别.我不会在与集合视图相同的视图控制器中使用tableview数据源和委托.

In your view controller you define a delegate and data source for your table view. Preferably, this is a different class. I would not have the tableview data source and delegate also in the same view controller as your collection view.

class WorkoutSettingsViewController: UIViewController, LoadWorkoutSettings {

    //MARK: - Properties
    //Used variables

    //Used constants
    private let settingsDelegate = SettingsTableViewDelegate()

扩展名将如下所示.

extension WorkoutSettingsViewController: UICollectionViewDataSource, UICollectionViewDelegate {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        //Whatever sets your sections
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //Whatever sets your rows per section
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Workout Settings", for: indexPath) as! SettingsCollectionViewCell

        settingsDelegate.workoutTitleLabel = [countdown, mainView, spokenMessage]
        settingsDelegate.mainContentLabel = getSettingsContent()
        cell.settingsTableView.delegate = settingsDelegate
        cell.settingsTableView.dataSource = settingsDelegate
        cell.settingsTableView.reloadData()

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

     //Whatever you need as header or footer
}

委托完全按照表视图数据源和委托的方式进行.

The delegate does exactly what you would like the table view data source and delegate to do.

class SettingsTableViewDelegate: NSObject, UITableViewDataSource, UITableViewDelegate {

    //MARK: - Properties
    //Used variables
    var workoutTitleLabel = [String]()
    var mainContentLabel = [String]()
    var selectedSetting: ((Int) -> ())? = .none
    private var secondaryContentLabel = [String]()

    //Used constants
    private let onTitle = NSLocalizedString("ON", comment: "Localized on title")
    private let offTitle = NSLocalizedString("OFF", comment: "Localized off title")
    private let fontColorBlack = UIColor(red: 20.0/255.0, green: 20.0/255.0, blue: 19.0/255.0, alpha: 1.0)
    private let fontColorRed = UIColor(red: 255.0/255.0, green: 96.0/255.0, blue: 89.0/255.0, alpha: 1.0)

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        workoutTitleLabel.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Settings Cell") as! WorkoutTableViewCell

        cell.workoutTitle.text = workoutTitleLabel[indexPath.row]
        cell.mainContent.text = mainContentLabel[indexPath.row]
        cell.secondaryContent.text = ""

        (mainContentLabel[indexPath.row] == offTitle) ? (cell.mainContent.textColor = fontColorRed) : (cell.mainContent.textColor = fontColorBlack)

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        selectedSetting?(indexPath.row)
    }

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

您的收藏夹视图单元格应如下所示.

Your collection view cell should look like this.

class SettingsCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var settingsTableView: UITableView!
}

这应该可以工作.如果需要从表视图委托/数据源到管理集合视图的视图控制器进行回调,则可以使用闭包.在示例表视图委托中,闭包称为selectedSettings.在viewDidLoad的视图控制器中,您可以像这样定义实例的回调:

This should then work. If you need to have a callback from the table view delegate / data source to your view controller managing your collection view, you can use a closure. In the example table view delegate the closure is called selectedSettings. In your view controller in viewDidLoad you define the call back for instance like this:

override func viewDidLoad() {
    super.viewDidLoad()

    settingsDelegate.selectedSetting = { [unowned self] selection in
        startSettingsMenu(for: selection)
    }

}

结果看起来像这样.

亲切的问候,MacUserT

Kind regards,MacUserT

这篇关于如何将TableView放入CollectionViewCell中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:18