Wouter
Wouter

Reputation: 807

How to access to a textLabel in a static cell

After much searching and reading I unfortunately do not come from the following. I want to use static tables to display certain data. (Are there better options?)

In my view I first put an onion picture with a container view underneath. The container view again refers to a Table View Controller.

I made an outlet from the cells and then I thought I could easily adjust the text.

Now I want to change the text of the fields in the table, but unfortunately I do not succeed.

When I start the app then the table is completely empty as seen on the screenshot.

What am I doing wrong ?

Overview

class TableViewController: UITableViewController {
    var data: [String] = ["Muis", "Aap", "Koe", "Vis"]

override func viewDidLoad() {

    super.viewDidLoad()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return data.count

}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let iets = data[indexPath.row]
    cell.textLabel?.text = iets

    return cell
    }
}

Upvotes: 0

Views: 852

Answers (4)

Wouter
Wouter

Reputation: 807

What I have done after a lot of testing and reading. I have create a segue to the statutable class.

if (segue.identifier == "myEmbeddedSegue") {
        let childViewController = segue.destination as! hondDetialTableViewController
        childViewController.hondId = hondData["hondId"]!
    }

In this segue I send only the hondId, everything else i ask entities.

Upvotes: 1

vadian
vadian

Reputation: 285150

If you want to use static cells

  • Forget dequeueing UITableViewCell instances and all tableview data source and delegate methods.
  • In Interface Builder select the table view and select Static Cells from the Content popup
  • Drag the amount of static cells you need into the canvas
  • In the view controller declare IBOutlets and connect them directly to the UI elements in the cells

Upvotes: 2

Francis.Beauchamp
Francis.Beauchamp

Reputation: 1373

You need to change your way of thinking for this one. You do not own the cells, the UITableView does. It will provide cells as it seems fit by using your implementations of UITableViewDataSource:

  • func numberOfSections(in: UITableView) -> Int
  • func tableView(UITableView, numberOfRowsInSection: Int) -> Int
  • func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

Normally, the texts (your actual data) would be held in a list available to this data source.

Example:

var data: [String] = []

// Other functions

func numberOfSections(in: UITableView) -> Int {
    return 1
}

func tableView(UITableView, numberOfRowsInSection: Int) -> Int {
    return data.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_IDENTIFIER")
    cell.text = data[indexPath.row]
    return cell
}

Now, if you want to change this cell's text, all you have to do is update your data list and reload the data.

Upvotes: 1

ekscrypto
ekscrypto

Reputation: 3816

I'm sorry but this is not at all how UITableView works. The UITableViewCell that you define in the Xib/Storyboard within the tableview are just "models" or templates, they don't actually exists until you dequeue them.

You can read how UITableView works here: http://www.thomashanning.com/uitableview-tutorial-for-beginners/

You have to return numberOfSections > 0 if you want anything displayed in your tableview; similarly, that section has to also have numberOfRows > 0 otherwise again, nothing will be displayed (ok, maybe headers and footers if those are properly setup).

At any rate, cells are only accessible after you dequeue them. Creating an outlet in a XIB to a UITableViewCell is useless in most cases.

You can explore other options, such as UIStackView, or maybe what you need is just plain custom UIView with labels that you properly set and layout using NSLayoutConstraints. There are plenty of resources out there, this is just one I quickly Googled for you to get started: https://www.youtube.com/watch?v=de0sthle44I

Good Luck.

Upvotes: 0

Related Questions