Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Stian Andreassen
Stian Andreassen
523 Points

Trying to learn about Structs (together with tableViews)

Hi

If trying to learn the ropes in Swift, and now I'm at Structs. I'm trying to make a basic app with a shopping cart. The app has a tableView, that passes information on to a new View Controller using the prepareForSegue method. So, the first tableView has a list of some foods. Hamburger, Pizza, Hot dog, Lasagne. When the user presses the Pizza cell, he is taken to a new View Controller that has some information about the dish, and one plussButton, and one minusButton. Then the user can select how many Pizza´s he want with the buttons. This should then be displayed in a tableView in another ViewController.

My problem is that I'm not sure how to get the counter to update in the cart tableView, also I'm struggling to remove the counter and the dish from the cart tableView when the counter is 0.

Here's some of the code: (P.S) this is for practice, hence the stupid naming.

Struct File:

struct test {
    var structName: String
    var structCount = Int()

}

var no1 = test(structName: "", structCount: Int())
var no2 = test(structName: "", structCount: Int())
var no3 = test(structName: "", structCount: Int())

var structNameArray = [String]()
var structCountArray = [Int]()

Code for plus and minusButtons:

 @IBAction func plussBUtton(_ sender: UIButton)
    {
        if selector == 1 {
            if no1.structCount == 0{
            no1.structCount += 1
            no1.structName = name
            structNameArray.append(no1.structName)
            structCountArray.append(no1.structCount)
            } else  {
                no1.structCount += 1
            }
         }

 @IBAction func minusButton(_ sender: UIButton)
    {
        if selector == 1 {
            no1.structCount -= 1
            if no1.structCount == 0 {
                //Remove both the counter and the name from the list
            }
        }

And in the cart TableView:

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

        //Set round corners
        //cell.cellView.layer.cornerRadius = cell.layer.frame.height / 2

         let name = structNameArray[indexPath.row]
         let count = structCountArray[indexPath.row]

        cell.checkOutLabel.text = "\(name)"
        cell.antalLabel.text = "\(count)"

        return cell
    }

I hope someone can help me out here :)