gpt4 book ai didi

swift - 图像未显示在表格 View 中

转载 作者:行者123 更新时间:2023-11-30 12:09:21 25 4
gpt4 key购买 nike

我正在尝试从 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!

}

最佳答案

我让一切都像这样工作。我不知道 for: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

}

关于swift - 图像未显示在表格 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46260180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com