mercredi 6 avril 2016

The Tableview only showing one cell

enter image description here

Function loadData is called inside viewDidLoad, but whenever the ViewController shows up; it only returns one cell.

Here's the actual code.

func loadData() {
    let filemgr = NSFileManager.defaultManager()
    let dirPaths = filemgr.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)

    databasePath = dirPaths[0].URLByAppendingPathComponent("products.db").path!

    let productsDB = FMDatabase(path: databasePath as String)

    if productsDB.open() {
        let querySQL = "SELECT ITEM, TOTAL_QUANTITY, TOTAL_PRICE, PRODUCT_IMAGE FROM PRODUCTS"

        let results:FMResultSet? = productsDB.executeQuery(querySQL, withArgumentsInArray: nil)

        if results?.next() == true {
            item = (results?.stringForColumn("ITEM"))!
            totalQuantity = Int((results?.stringForColumn("TOTAL_QUANTITY"))!)!
            totalPrice = Double((results?.stringForColumn("TOTAL_PRICE"))!)!
            productImage = (results?.stringForColumn("PRODUCT_IMAGE"))!

            let products: [String: Any] = ["item": item, "totalQuantity": totalQuantity, "totalPrice": totalPrice, "productImage": productImage]

            cartItems.append(products)

        }
        productsDB.close()
        myTableView.reloadData()
    } else {
        print("Error: \(productsDB.lastErrorMessage())")
    }
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath) as! CartCell

    cell.productTitle.text = String(cartItems[indexPath.row]["item"]!)
    cell.productPrice.text = String(cartItems[indexPath.row]["totalPrice"]!)
    cell.productQuantity.text = String(cartItems[indexPath.row]["totalQuantity"]!)

    let imageURL = String(cartItems[indexPath.row]["productImage"]!)
    let url = NSURL(string: imageURL)
    cell.productImage.sd_setImageWithURL(url)

    return cell
}

What do you think is missing in this code, or is there something wrong with where I put the reloadData function?

Aucun commentaire:

Enregistrer un commentaire