SwiftUI 列表未使用 ForEach 正确更新
SwiftUI List Not Updating Correctly with ForEach
我正在尝试使用带有两个 ForEach 循环的列表来创建收藏夹功能。当我点击按钮时,项目移动到正确的部分,但按钮图像和功能没有更新。有没有我遗漏或做错了什么?
如果我导航到另一个视图并返回,最初会正确呈现列表,但如果我单击按钮仍会表现出相同的行为。
如果我使用两个具有完全相同设置的单独列表,一切正常,但在 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 {[=10=] != business}
self.state.others.append(business)
}
else{
self.state.others = self.state.others.filter {[=10=] != business}
self.state.favorites.append(business)
}
sortBusinesses()
}
func sortBusinesses(){
self.state.favorites.sort {
[=10=].name < .name
}
self.state.others.sort {
[=10=].name < .name
}
}
}
您是否已确认正在调用 .onTapGesture
?或者,您可以将 self.toggleFavorite(business: favorite)
放在按钮的操作内。
Button(
action: { self.toggleFavorite(business: business) },
label: { Image(systemName: "heart") }
)
更新的答案,有效,但是...
1) 诀窍是每当您将 "cell" 从收藏夹更改为 non-favorite 时更改 id -> 我假设 Apple 使用 UITableView - dequeueResubleCell 的逻辑,如果 id 相同不会更新,只是 reused/copied 所以心没有变
2) 我现在在收藏夹更改时随机更改 ID - 你必须在那里考虑 better/cleaner 解决方案。
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 {[=10=] != 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 {[=10=] != 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 {
[=10=].name < .name
}
self.state.others.sort {
[=10=].name < .name
}
}
}
我正在尝试使用带有两个 ForEach 循环的列表来创建收藏夹功能。当我点击按钮时,项目移动到正确的部分,但按钮图像和功能没有更新。有没有我遗漏或做错了什么?
如果我导航到另一个视图并返回,最初会正确呈现列表,但如果我单击按钮仍会表现出相同的行为。
如果我使用两个具有完全相同设置的单独列表,一切正常,但在 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 {[=10=] != business}
self.state.others.append(business)
}
else{
self.state.others = self.state.others.filter {[=10=] != business}
self.state.favorites.append(business)
}
sortBusinesses()
}
func sortBusinesses(){
self.state.favorites.sort {
[=10=].name < .name
}
self.state.others.sort {
[=10=].name < .name
}
}
}
您是否已确认正在调用 .onTapGesture
?或者,您可以将 self.toggleFavorite(business: favorite)
放在按钮的操作内。
Button(
action: { self.toggleFavorite(business: business) },
label: { Image(systemName: "heart") }
)
更新的答案,有效,但是...
1) 诀窍是每当您将 "cell" 从收藏夹更改为 non-favorite 时更改 id -> 我假设 Apple 使用 UITableView - dequeueResubleCell 的逻辑,如果 id 相同不会更新,只是 reused/copied 所以心没有变
2) 我现在在收藏夹更改时随机更改 ID - 你必须在那里考虑 better/cleaner 解决方案。
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 {[=10=] != 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 {[=10=] != 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 {
[=10=].name < .name
}
self.state.others.sort {
[=10=].name < .name
}
}
}