2

I have a struct/model that I store some JSON data to fill my tableview:

import SwiftyJSON

struct MyData {

    let valOne: Int
    let valTwo: String
    let valThree: Int

    init(json: JSON) {
        valOne = json["some_data"].intValue
        valTwo = json["auc_data"].stringValue
        valThree = json["auc_data"].intValue
    }
}

Then in my tableview I have a custom cell class that I use when I write out my data:

let data = MyData[indexPath.row]

let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! CustomTableViewCell

cell.configureCellWithData(data)

What I want to do is to pass the struct as a parameter to the configureCellWithData() but I am not sure how to declare it.

Instead of doing something like:

func configureCellWithData(dataOne: Int, dataTwo: String, dataThree: Int) {

}

then

configureCellWithData(data.valOne,data.valTwo,data.valThree)

I dont want to have many parameters instead I want to send the struct right away

2
  • 1
    Have you tried this way ? func configureCellWithData(data: MyData) { } Commented Oct 20, 2016 at 2:44
  • The let data = MyData[indexPath.row] line doesn't make sense. I assume you have some property defined like var dataObjects = [MyData]() or something like that, and you'd then do let data = dataObjects[indexPath.row]. Commented Oct 20, 2016 at 2:55

1 Answer 1

5

Try this.

func configureCellWith(data: MyData) {
    // Do stuff here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, or if Swift 3, perhaps func configureCell(with data: MyData) { ... }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.