如何解决在 SwiftUI 列表中重新加载单个行属性
我试图让用户有机会保存或取消保存新闻文章。保存按钮将其保存在后端,但当后端更新时,图像不会更新。
@State var industryInsights = [IndustryInsight]()
List {
ForEach(industryInsights,id: \.self) { insight in
IndustryInsightRowView(insight: insight)
}
}
我不确定这样做的最佳方法,但我希望按钮图像在用户按下按钮时更新。任何帮助将不胜感激。
InsightClass 和属性
struct IndustryInsight: Codable,Hashable,Identifiable {
let id: Int
let industry,alert,date: String
let url: String
let status: String
}
按下保存按钮时的功能。
func saverowButtonClicked(alert: IndustryInsight) {
if alert.status == "Saved" {
RestAPI.changeStatusIndustryAlerts(id: alert.id,status: "Unsave",completionHandler: { () in
})
} else {
RestAPI.changeStatusIndustryAlerts(id: alert.id,status: "Save",completionHandler: { () in
})
}
}
解决方法
我尽量避免使用@State,而是使用@ObservableObject 和@Published。 尝试将您的 IndustryInsight 结构转换为 ObservableObject 类,并让 IndustryInsightRowView 作为 @ObservedObject 接受它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。