Tableview仅显示一个单元格

时间:2016-04-06 08:49:52

标签: xcode swift sqlite uitableview reloaddata

enter image description here

函数loadData在viewDidLoad中调用,但每当ViewController出现时;它只返回一个单元格。

这是实际代码。

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
}

您认为此代码中缺少什么,或者我放置reloadData函数的位置有什么问题?

0 个答案:

没有答案