UITableViewController filling cells of different s

2019-08-22 13:53发布

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

enter image description here

and this is the corresponding TableViewCell file,

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

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()
        }
    }
}

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)

UPDATE: this is an overview of my TableViewController

enter image description here

Thank you in advance and sorry for such a long question

2条回答
甜甜的少女心
2楼-- · 2019-08-22 14:19

Once you set up all prototype cell classes and reuse identifiers, you can use below code to generate the cells. Since you didn't include the exact code for your classes, this is just a skeleton code, but you should be able to fill in the gaps.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.section {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: "AllgemeineProduktdaten") as! AllgemeineProduktdatenCell
        cell.setUpAllgemeineFields(product: product_obj)
        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: "OtherCellReuseIdentifier") as! OtherCellClass
        //set up cell content
        return cell
    default:
        return UITableViewCell()
    }
}
查看更多
相关推荐>>
3楼-- · 2019-08-22 14:40

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
        }
    }
}

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

查看更多
登录 后发表回答