How to populate table view with custom object?

uitableviewcell
populate uitableview with array swift 4
add data to table view swift
swift settings table view
static table view swift 4
how to create a table view controller
how to add table view section in storyboard
table view data source

I have a struct that conforms to the Codable protocol. I make an URLSession request and receive an JSON response. I encode the response into my custom struct that looks something like this.

struct Weather: Codable {
    let time: Date
    let location: String
    let timeEvents: [TimeEvents]
}  

struct TimeEvents: Codable {
    let validTime: Date
    let parameters: [Parameters]
}

struct Parameters: Codable {
    let name: String
    let unit: String
    let values: [Double]
}

I get a single object Weather. However, inside the object Weather I want to populate the validTime, i.e. 01/01/2018 12:00, with the corresponding name matching "temperature" and corresponding values, i.e. 10.5 C.

So the rows will look something like:

[01/01/2018 12:00 : 10.5C]
[01/01/2018 13:00 : 11.5C]
[01/01/2018 14:00 : 11.6C]
.
.
.
[02/01/2018 00:00 : 3.1C]
[02/01/2018 01:00 : 3.1C]

In my controller I have something like:

var testWeatherObject : [Weather] = []

I only want a single header at the top displaying a single location i.e. New York.

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return testWeatherObject[section].timeEvents.count
}

The above crashes my app. I'm not sure how to get the correct row counts.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "WeatherDataCell", for: indexPath)

    return cell
}

You may access an empty array so

return testWeatherObject.first?.timeEvents.count ?? 0

Filling a Table with Data, method to retrieve a cell object. Configure the cell's views with your app's custom data. Return the cell to the table view. For standard cell  Most examples only show how to populate the table by manual input from the user, but I need to generate my data with a method and fill it. I definitely don't want to create a different setter method per table column, I want to easily change the table size if needed: 3x3, 6x6, 20x20, etc.

If you don't have sections you don't need to implement numberOfSections(in tableView: UITableView)

If you're only working with a single Weather object why not change the definition of testWeatherObject to

var testWeatherObject = [TimeEvents]

and your numberOfRowsInSection would then become

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

Start Developing iOS Apps (Swift): Create a Table View, To configure a custom cell for your table view You may have to disclose those objects to see the table view cell. You still need to create instances of this cell in code and fill them with your app's  I populate gt_contracts in DO_PREPARE_OUTPUT and it works (i.e. the table in the view is populated). However, this is not the proper way as for example the button to download to Excel does not show up, even if the property is set to TRUE.

If you want to have just single section, you don't have to set this. Delete these lines of code:

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

And now if you want to have single table view cell for each timeEvent you should create your weather object

var testWeatherObject = // your Weather object

and in data source method numberOfRowInSection just say I want as many cells as testWeatherObject has timeEvents

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return testWeatherObject.timeEvents.count
}

However I recommend not to use your Weather object as variable. Instead you should create array of TimeEvents

var timeEvents = [/* your TimeEvents objects*/]

and in table view data source method use just this

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

Two Basic Ways To Populate Your UITableView, There are two main base ways to populate a tableview. this object to type UITableViewCell , since we can use a custom type (inheriting from  I need to populate my table view with the data from my core data. I can display my rows but, I can't populate my tableview! I use Swift 3 and Xcode 8. This is my code, in this controller I have to

It’s crashing because you’re trying to access a nested array that is empty.

I would check to see if testWeatherObjects.count is 0 first. If not then you can go ahead and use testWeatherObject[section].timeEvents.count

Using Custom cells to customize UITableViewCells, Custom in a Storyboard to customize the views of the UITableView Select the Image View and configure the Content Mode Aspect Fill so  I want to populate my Android spinner with data from a SQLite table. I already have the database controllers, and these are returning custom objects. Now what I need to do is populate the spinner with this objects, in order to get not the string field, but the Id field. For example: My table Days, with fields Id and Day: id - day 1 - Sunday. 2

Does the identifier of cell defined in storyboard or xib match "WeatherDataCell"?

I made a simple tableview app sample. https://github.com/y-okudera/TableViewDemoApp

I hope to be helpful.

iOS custom Table Views with Cells - Q agency, Table Views are a big part of the iOS, and being able to create a fully with a UITableViewCell object that the table view's delegate creates with the As you can see our tableView is populated with four custom made cells  Generically populate a custom .NET class from a SqlDataReader Introduction If you have ever written code that reads data from a table, then you will be familiar with having to map the columns from your SqlDataReader to your custom .NET class.

Customize Table View Cells for UITableView, Customize the table view cell – instead of using the default style of In the Object Library, select “Table View Cell” and drag it to the design area of the Interface Builder. Fill in “SimpleTableCell” for the class name and select  notice that i had to declare my newly created objects and my array in a " func setup()" method because they were not recognized as "Continent" object but as a "FirstTableViewController" object now my goal is to populate the following table view with the first property of each object being the "name", and i used the following code:

iOS 4 Programming Cookbook: Solutions & Examples for iPhone, iPad, , root view controller, 144, 461 switching views, 61 tab bars, 71 using views in 277 custom accessories, table views, 103–105 custom objects, 2–5 custom pins, in table views, 127–138 loading, 456 populating table views, 94–98 retrieving​  In this video we will learn, how to use the default ArrayAdapter to fill a spinner with objects of our own custom Java class. The spinner will only populate one TextView with the information we

Easy TableView Setup Tutorial, Step 2: In our ViewController class, instantiate a UITableView object Create a tableViewCell to populate the screen. images, and custom views), managing the cell selection and highlight state, managing accessory views,  In the Object Library, type tableview and drag it onto the View Controller as shown below. This will place the tableview on top of the View Controller. Your Document Outline will also look like the following: Steps Involved When Using Interface Builder. This method starts with dragging a Table View Cell from the Object Library onto your TableView.

Comments
  • Didn't you forget to register cell in viewDidLoad?
  • It's rare to see an up-voted question with four answers, none of which are accepted. More, you haven't indicated that anything helped. Care to offer some input?
  • @dfd just had time to go through them. Already accepted an answer. Thanks