Hi, New to Swift, E-commerce app

products
swift

#1

Hi, New to Swift, E-commerce app, have been following a youtube video from CodeWithChris -> https://www.youtube.com/watch?v=lim3t6xdbXg <- but it seems like his version of xcode is a bit outdated. I wanted to create something similar to his idea but don’t know how. The following code isn’t showing any errors but displays product information stored in moltin store json format and not on my TableView. I would appreciate any help to show this information on my TableView.Sorry for my bad grammar:)

import UIKit
import Moltin

class MasterViewController: UITableViewController {

    var detailViewController: DetailViewController? = nil
    var objects = [Any]()
    


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        Moltin.clientID = "xxxxxxxxxxx"
      
        
        
        if let split = self.splitViewController {
            let controllers = split.viewControllers
            self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
        }
        
        Moltin.product.list() { result in
            switch result {
            case .failure(let error):
                print("Failed to get products:\n\(error)")
            case .success(let productList):
                print("Got products \(productList.products)")
                
                
            self.tableView.reloadData()
            }
        }
        
        
    }

    override func viewWillAppear(_ animated: Bool) {
        self.clearsSelectionOnViewWillAppear = self.splitViewController!.isCollapsed
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func insertNewObject(_ sender: Any) {
        objects.insert(NSDate(), at: 0)
        let indexPath = IndexPath(row: 0, section: 0)
        self.tableView.insertRows(at: [indexPath], with: .automatic)
    }

    // MARK: - Segues

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let object = objects[indexPath.row] as! NSDate
                let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }
    }

    // MARK: - Table View

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

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

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

        let object = objects[indexPath.row] as! [String:Any]
        cell.textLabel!.text = object["name"] as? String
        return cell
    }

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            objects.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }
    }


}

#2

Hi,

Looks like you’re using v2 of the iOS SDK, but the CodeWithChris tutorials use v1. You can find an example of how to load products into a table view or collection view in the example application for the v2 branch of the SDK, here’s the product list we have in that application for example:

This product list uses a UICollectionView rather than a UITableView, but the core premise remains the same.

There’s a few key things you need to update:

  • Tweak the objects property to be of type [Product] rather than [Any]
  • Store the products you’re getting in the success case to self.objects rather than just printing them out.
  • Update tableView(:cellForRowAt:indexPath:) to grab the object from that objects property as a Product, rather than [String:Any], and get the products name as product.name.
  • Update prepare(for:sender:) to grab the object from that objects property as a Product rather than NSDate
  • Check whether you need insertNewObject - that’s attempting to create new items which wouldn’t likely be needed as these come from the SDK.

(We recently released v3 of the SDK too, which is on the master branch, it’s not too dissimilar to what you have now so might be worth taking a look, let us know what you think!)

Hope this helps,
Craig


#3

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.