2017-09-17 102 views
0

我想要從mysql的項目列表,並顯示它們在應用程序中。 名稱出現,但圖像和價格沒有。圖像沒有顯示在表查看

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // let cell = tableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath) 
    var cell:UITableViewCell? = 
     tableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath) 
    if (cell == nil) 
    { 
     cell = UITableViewCell(style: UITableViewCellStyle.subtitle, 
           reuseIdentifier: "BasicCell") 
    } 

    // Get the location to be shown 
    let item: Location = locations[indexPath.row] 
    // let price=locations[indexPath.row].price 

    let price=(item.price as NSString).floatValue 
    let cellPrice = String(format: "%.2f",price) 
    let serverurl=NSURL(string: "https://myoscapp.com/boot/images/") 
    let imageaddress = locations[indexPath.row].image 
    let imageURL = NSURL.init(string: imageaddress, relativeTo: serverurl! as URL) 
    cell?.textLabel?.text=locations[indexPath.row].name 
    cell?.detailTextLabel?.text = cellPrice 
    cell?.imageView?.sd_setImage(with: imageURL! as URL) 
    cell?.setNeedsLayout() //invalidate current layout 
    cell?.layoutIfNeeded() //update immediately 
    return cell! 

} 
+0

我認爲'imageURL作爲URL?'應該是'imageURL as! URL'作爲函數期望一個'URL'不是一個可選的'URL' – 3stud1ant3

+1

它會被改變,如果你嘗試一個硬編碼的虛擬url? –

+0

1.打印您的物品。它是否包含價格值? 2.爲圖像打印imageURL,並在瀏覽器中打開此URL,以便看到url是否無效。 – Woof

回答

0

我得到了一切這樣工作。我不知道是否對於:indexPath會產生效果,但我什麼都看不到

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // let cell = tableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath) 
    let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, 
           reuseIdentifier: "BasicCell") 
    // Get the location to be shown 
    let item: Location = locations[indexPath.row] 
    // let price=locations[indexPath.row].price 

    let price=(item.price as NSString).floatValue 
    let cellPrice = String(format: "%.2f",price) 
    let serverurl=NSURL(string: "https://myoscapp.com/boot/images/") 
    let imageaddress = locations[indexPath.row].image 
    let imageURL = NSURL.init(string: imageaddress, relativeTo: serverurl! as URL) 
    cell.textLabel?.text=locations[indexPath.row].name 
    cell.detailTextLabel?.text = cellPrice 
    cell.imageView?.sd_setImage(with: imageURL! as URL) 
    cell.setNeedsLayout() //invalidate current layout 
    cell.layoutIfNeeded() //update immediately 
    return cell 

}