我正在尝试使用带有两个 ForEach 循环的 List 创建收藏夹功能。当我点击按钮时,项目移动到正确的部分,但按钮图像和功能没有更新。我有什么遗漏或做错了吗?
如果我导航到另一个 View 并返回,列表最初会正确呈现,但如果我单击按钮仍会显示相同的行为。
如果我使用两个具有完全相同设置的单独列表,一切正常,但这在 UI 中看起来很奇怪,因为即使列表中没有项目,每个列表也占用相同数量的空间。
或者,有没有办法强制列表作为按钮点击的一部分重新绘制?
谢谢!
import SwiftUI
struct BusinessListView: View {
@EnvironmentObject var state: ApplicationState
var body: some View {
VStack{
List {
Section(header: Text("Favorites")) {
if(state.favorites.count == 0){
Text("No Favorites")
}
else {
ForEach(state.favorites, id: \.self) { favorite in
HStack{
Button(
action: {},
label: { Image(systemName: "heart.fill").foregroundColor(.red) }
)
.onTapGesture { self.toggleFavorite(business: favorite)}
Text("\(favorite.name)")
}
}
}
}
Section(header: Text("Other Businesses")) {
ForEach(state.others, id: \.self) { business in
HStack{
Button(
action: {},
label: { Image(systemName: "heart") }
)
.onTapGesture { self.toggleFavorite(business: business)}
Text("\(business.name)")
}
}
}
}
}
}
func toggleFavorite(business: Business){
if(state.favorites.contains(business)){
self.state.favorites = self.state.favorites.filter {$0 != business}
self.state.others.append(business)
}
else{
self.state.others = self.state.others.filter {$0 != business}
self.state.favorites.append(business)
}
sortBusinesses()
}
func sortBusinesses(){
self.state.favorites.sort {
$0.name < $1.name
}
self.state.others.sort {
$0.name < $1.name
}
}
}
最佳答案
更新的答案,有效,但是......
1)诀窍是每当您将“单元格”从喜欢的更改为非喜欢的时更改 id -> 我假设 Apple 使用 UITableView - dequeueResubleCell 的逻辑,如果 id 相同,则不会更新,而只是重用/copyed 所以心没有改变
2)我现在在收藏夹更改时随机更改 ID - 您必须在那里考虑更好/更清洁的解决方案。
struct BusinessListView: View {
@EnvironmentObject var state: ApplicationState
var body: some View {
VStack{
List {
Section(header: Text("Favorites")) {
if(state.favorites.count == 0){
Text("No Favorites")
}
else {
ForEach(state.favorites, id: \.self) { favorite in
HStack{
Button(
action: {
self.toggleFavorite(business: favorite)
},
label: {
HStack {
Image(systemName: "heart.fill").foregroundColor(.red)
Text("\(favorite.name)")
}
}
).id(favorite.id)
}
}
}
}
Section(header: Text("Other Businesses")) {
ForEach(state.others, id: \.self) { business in
HStack{
Button(
action: {
self.toggleFavorite(business: business)
},
label: {
HStack {
Image(systemName: "heart")
Text("\(business.name)")
}
}
)
.id(business.id)
}
}
}
}
}
}
func toggleFavorite(business: Business){
if(state.favorites.contains(business)){
self.state.favorites = self.state.favorites.filter {$0 != business}
self.state.others.append(business)
if let index = self.state.others.index(of: business) {
self.state.others[index].id = Int.random(in: 10000...50000)
}
}
else{
self.state.others = self.state.others.filter {$0 != business}
self.state.favorites.append(business)
if let index = self.state.favorites.index(of: business) {
self.state.favorites[index].id = Int.random(in: 10000...50000)
}
}
sortBusinesses()
}
func sortBusinesses(){
self.state.favorites.sort {
$0.name < $1.name
}
self.state.others.sort {
$0.name < $1.name
}
}
}
关于swiftui-list - SwiftUI 列表未使用 ForEach 正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62048265/