提问人:Nus 提问时间:10/31/2023 最后编辑:CheolhyunNus 更新时间:10/31/2023 访问量:45
无法显示列表视图中的图像
Unable to display the Image from List view
问:
我有一个简单的 SwiftUI 购物车应用程序。在主视图应用程序中。我有产品宽度列表添加到购物车按钮。我希望当用户单击购物车按钮时,它应该添加商品属性,如图像、价格、名称等。当我单击添加到购物车按钮时,我只能显示文本字段,但图像字段无法添加到购物车视图中。
主要问题出在OrderView中。item.thumbnail 未显示图像。
Image(item.thumbnail)
.resizable()
.frame(width: 100, height: 100)
.clipShape(Circle())
这是我的订单类。
import SwiftUI
class Order: ObservableObject {
@Published var product = [Product]()
var total: Int {
if product.count > 0 {
return product.reduce(0) { $0 + $1.price }
} else {
return 0
}
}
func add(item: Product) {
product.append(item)
}
func remove(item: Product) {
if let index = product.firstIndex(of: item) {
product.remove(at: index)
}
}
}
这是我的主页(产品列表视图)视图。这是主要代码,但如果需要,我可以添加整个代码。
VStack {
if viewModel.productLists.count > 0 && !viewModel.refreshing {
List(viewModel.productList, id: \.self) { product in
ProductListViewCell(productData: product)
} .listStyle(.grouped)
}
}
下面是 ProductListViewCell 代码。
struct ProductListViewCell: View {
let productData: Product
@EnvironmentObject var order: Order
var body: some View {
HStack {
if let url = URL(string: productData.thumbnail) {
ProductAsyncImageView(url: url)
.frame(width: 150, height: 150)
.mask(RoundedRectangle(cornerRadius: 16))
}
VStack(alignment: .leading,spacing: 5) {
Text("Name: " + (productData.title))
.frame(maxWidth: .infinity, alignment: .leading)
.font(.headline)
Text("Description: " + (productData.description))
.frame(maxWidth: .infinity, alignment: .leading)
Text("Price: £" + String(productData.price))
.frame(maxWidth: .infinity, alignment: .leading)
.font(.subheadline)
Button {
order.add(item: productData)
} label: {
HStack {
Image(systemName: "cart.badge.plus")
Text("Add to Cart")
.fontWeight(.semibold)
}
.foregroundColor(.white)
.frame(width: 150 , height: 40)
}
.background(Color(.systemBlue))
.cornerRadius(10)
}
}
}
}
这是订单视图代码。
import SwiftUI
struct OrderView: View {
@EnvironmentObject var order: Order
var body: some View {
NavigationStack {
List {
Section {
ForEach(order.product) { item in
HStack {
Image(item.thumbnail)
.resizable()
.frame(width: 100, height: 100)
.clipShape(Circle())
VStack {
Text(item.title)
Text("£\(item.price) | \(item.discountPercentage)")
Button("Show details") {
}.foregroundColor(.gray)
}
}
}
}
Section {
NavigationLink("Place Order") {
Text("Check out")
}
}
}
.navigationTitle("Order")
}
}
}
下面是产品列表视图的屏幕截图。
答:
1赞
Gizem Turker
10/31/2023
#1
item.thumbnail 似乎有问题。这部分可以先调试。图像可以显示在 ProductListViewCell 文件中,但不能显示在 OrderView 文件中。请检查代码并更新。
也许您可以更新 OrderView:
import SwiftUI
struct OrderView: View {
@EnvironmentObject var order: Order
var body: some View {
NavigationView {
List {
Section {
ForEach(order.product) { item in
HStack {
if let url = URL(string: item.thumbnail) {
ProductAsyncImageView(url: url)
.frame(width: 100, height: 100)
.clipShape(Circle())
}
VStack(alignment: .leading) {
Text(item.title)
.font(.headline)
Text("£\(item.price) | \(item.discountPercentage)")
.font(.subheadline)
Button("Show details") {
// actions
}
.foregroundColor(.gray)
}
}
}
}
Section {
NavigationLink("Place Order") {
Text("Check out")
}
}
}
.navigationTitle("Order")
}
}
}
评论
0赞
Nus
10/31/2023
谢谢你的回答。如何使用 ProductAsyncImageView 应用可调整大小的属性?
0赞
Gizem Turker
10/31/2023
可以将其添加到 ProductAsyncImageView。
0赞
Nus
10/31/2023
我有这个错误 'some View' 类型的值没有成员 'resizable' 。当我尝试添加到 OrderView .但是我将属性放入 ProductAsyncImageView 中。
0赞
Nus
10/31/2023
我添加了屏幕截图
0赞
Gizem Turker
10/31/2023
您可以使用填充、间距 ..等
下一个:检索购物车中的商品
评论