UITableViewController 用值填充不同部分的单元格 [英] UITableViewController filling cells of different sections with Values

查看:33
本文介绍了UITableViewController 用值填充不同部分的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定之前有人问过这个问题,但我找不到解决方案.我有一个带有静态 cellsUITableViewController,我有 4 个 sections(到目前为止),每个 section 只有 1 个单元格.

I'm sure this question is asked before but I couldn't find a solution for it. I have a UITableViewController with static cells, I have 4 sections (so far) and each section only has 1 cell.

这是第一部分的第一个单元格的一部分

this is a part of the first Cell of First section

这是对应的TableViewCell文件,

and this is the corresponding TableViewCell file,

AllgemeineProduktdatenCell.swift

AllgemeineProduktdatenCell.swift

class AllgemeineProduktdatenCell: UITableViewCell{

    // MARK: - Outlets
    @IBOutlet weak var AngebotsnameTextF: UITextField!
    @IBOutlet weak var Referenznummer: UITextField!
    @IBOutlet weak var modelTextF: UITextField!
    @IBOutlet weak var bemerkungTextF: UITextView!
    @IBOutlet weak var extrasTextF: UITextView!
    @IBOutlet weak var ursprLadenPreisEuroTextF: UITextField!
    @IBOutlet weak var ursprLadenPreisCentTextF: UITextField!
    @IBOutlet weak var AngebotsPreisEuroTextF: UITextField!
    @IBOutlet weak var AngebotsPreisCentTextF: UITextField!

    // Bereich Möbel
    @IBOutlet weak var bereichMoebelLabel: UILabel!
    @IBOutlet weak var bereichMoebelPicker: UIPickerView!
    @IBAction func showHideBereichMoebelPicker(_ sender: Any) {
        if bereichMoebelPicker.isHidden{
            bereichMoebelPicker.isHidden = false
        } else{
            bereichMoebelPicker.isHidden = true
        }
    }

    // Hersteller
    @IBOutlet weak var chosenHerstellerLabel: UILabel!
    @IBOutlet weak var herstellerPickerView: UIPickerView!
    @IBAction func showHideHerstellerPicker(_ sender: UIButton) {
        if herstellerPickerView.isHidden{
            herstellerPickerView.isHidden = false
        } else{
            herstellerPickerView.isHidden = true
        }
    }

    // Anzeigen auch auf eigener Webseite
    @IBOutlet weak var anzeigenAuchAufEigenerWebseiteSwitch: UISwitch!
    @IBAction func anzeigenAufWebsiteSwitch(_ sender: Any) {
    }

    // Start des Angebot
    @IBOutlet weak var startDesAngebotChosenDateLabel: UILabel!
    @IBOutlet weak var startDesAngebotDatePicker: UIDatePicker!
    @IBAction func hideShowStartDesAngebotDP(_ sender: Any) {
        if startDesAngebotDatePicker.isHidden{
            startDesAngebotDatePicker.isHidden = false
        } else{
            startDesAngebotDatePicker.isHidden = true
        }
    }



    func setUpAllgemeineFields(product: ItemsClass){
        AngebotsnameTextF.text = "ABED"//product.title
        Referenznummer.text = product.artikel_nummer
        modelTextF.text = product.typ_kategorie
    }

}

想象一下,我的每个单元格都有类似的文件.

Imagine that I have similar files for each of my cells.

这是我的主要视图文件:

and this is my main View file:

AddEditVC.swift

AddEditVC.swift

class AddEditVC: UITableViewController {

    var product_obj: ItemsClass!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "AllgemeineProduktdaten") as? AllgemeineProduktdatenCell{
            cell.setUpAllgemeineFields(product: product_obj)
            return cell
        } else{
            print("FFFFFFAAAAAILD")
            return AllgemeineProduktdatenCell()
        }
    }
}

当我运行这个应用程序时,单元格是空白的,我什至看不到我的 Views 但是当我注释掉 cellForRowAt 函数时,我可以看到我的 出口,但字段为空.

when I run this application the cell is blank and I even cannot see my Views but when I comment out the cellForRowAt function then I can see my outlets but the fields are empty.

如何用对象填充单元格的字段?我应该如何指定要填充的单元格(我的其他部分的其他单元格也需要它)

How can I fill the fields of my cell with my object? how should I specify that which cell to be filled (I need it also for my other cells of other sections)

更新:这是我的 TableViewController

先谢谢你,抱歉问这么长的问题

Thank you in advance and sorry for such a long question

推荐答案

我设法通过询问另一个 问题André 的帮助下,我使用了垂直 UIStackView 并在第一行添加一个按钮(以及其他行中的任何元素),然后将 IBAction 添加到 UIButton 的代码中,如下所示:

I managed to do it by asking another question with the help of André, I used a Vertical UIStackView and added a button in first row (and whatever element in the as other rows), then add an IBAction to the code for the UIButton as follow:

@IBAction func Section1Tapped(_ sender: Any) {
    guard let stackView = (sender as AnyObject).superview as? UIStackView else { return }
    guard stackView.arrangedSubviews.count > 1 else { return }
    let sectionIsCollapsed = stackView.arrangedSubviews[1].isHidden

    UIView.animate(withDuration: 0.25) {
        for i in 1..<stackView.arrangedSubviews.count {
            stackView.arrangedSubviews[i].isHidden = !sectionIsCollapsed
        }
    }
}

这将使StackView崩溃,无需经历TableViews

This will make the StackView to collapse, no need to go through complexity of TableViews

这篇关于UITableViewController 用值填充不同部分的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆