函数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函数的位置有什么问题?