-1

I have an object which has properties and I want to display each property as tableview row. How should I prepare my datasource

For eg:

struct Car {
    var make: String
    var model: String
    var year: String
    var vin: String
}

let ford = Car(make: Ford, model: Mustang, year: 2018, vin: 1234)

Now in my tableview i need to display like below i.e., each property as a table row cell

enter image description here

How do I prepare my data source?

I understand that I need to add this to array with propertyname: value like

["make":"Ford", "model":"Mustang", ...]

but how can i do that and how can i parse it to display in my cellForRow method.

Please advice

This is just sample. In my actual struct I have more than 20 properties and each needs to be display in table row. So wanted this to be in tableview

Ashh
  • 569
  • 1
  • 6
  • 28
  • 1
    Your question is pretty broad... Where is your data? Are you getting it as JSON data from a server? Is it in a local database? A local text file? – DonMag Apr 02 '19 at 16:25
  • I get my data from json and parse it to another model object. json contains array of cars and on tap on that car this page shows up – Ashh Apr 02 '19 at 16:43
  • So, you're already parsing your json data to an array of `Car` objects? Not sure where you're having problems then? Review [ask] and [mcve]. – DonMag Apr 02 '19 at 16:50
  • please look at the question again..this is nothing to do with json. Also I have array of car objects thats correct. Each car object has the model that I mentioned in question. This question is for single car not for all cars. From the model structure above how can i display table view – Ashh Apr 02 '19 at 16:56
  • I initially closed this as a duplicate of https://stackoverflow.com/questions/46597624/can-swift-convert-a-class-struct-data-into-dictionary which shows how to make a dictionary from a struct (because of your [comment](https://stackoverflow.com/questions/55479321/good-way-to-display-tableview-from-object-ios-swift?noredirect=1#comment97669609_55479586)), but I've reopened because *no*, that is not what you need to do. You already have a model of your data, so you don't need to make a dictionary from it in order to display in a tableview. Just use the model. – Eric Aya Apr 02 '19 at 17:15
  • its bit confusing but my exact question is object "car" has make, model, year, vin as properties.. how can i display that in table view with each property as row. Please advice – Ashh Apr 02 '19 at 17:19

2 Answers2

1

Assuming you know your structure...

Create a custom table view cell, with a "property name" label and a "value" label (as shown in your image).

Add class variables:

let propertyArray = [
    "Make",
    "Model",
    "Year",
    "VIN"
]

var valueArray: [String] = [String]()

when you've selected a Car object,

valueArray = [
    selectedCarObject.make,
    selectedCarObject.model,
    selectedCarObject.year,
    selectedCarObject.vin
]

Now, your tableView implementation can use:

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

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

    cell.propertyLabel.text = propertyArray[indexPath.row]
    cell.valueLabel.text = valueArray[indexPath.row]

    return cell
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • 1
    Yes, but it's even better to have an array of cars `var cars: [Car] = ...` and then access the properties from these objects, like `cell.propertyLabel.text = cars[indexPath.row].make`, etc. – Eric Aya Apr 02 '19 at 18:43
  • @ayaio - presumably the OP *has* an array of cars, and is passing the "selected" car to whatever class he's using to display the table of properties. – DonMag Apr 02 '19 at 18:45
0
var modelDictionary = ["make":"Ford", "model":"Mustang", ...]

Your modelDictionary will be your datasource, im assuming your cell has two label which you will put the data of key and value of the dictionary.

how will you make an Dictionary of your model data?

You can try creating a function on your model to create an Dictionary for each property it has.

Hope i answer you question.

  • Hi John, Im actually looking for sample code which converts my model data to dictionary – Ashh Apr 02 '19 at 16:45