Reputation: 53
I am attempting to create a favorites functionality using a List with two ForEach loops. When I click on the button, the item moves to the correct section, but the button image and functionality are not updated. Is there something I am missing or doing incorrectly?
If I navigate to another view and back, the list is rendered correctly initially, but still exhibits the same behavior if I click on the button.
If I use two separate lists with exactly the same setup, everything works fine, but that looks strange in the UI because each list takes up the same amount of room even if there are no items in the list.
Alternatively, is there a way to force the list to redraw as part of the button tap?
Thanks!
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
}
}
}
Upvotes: 5
Views: 2629
Reputation: 8091
UPDATED answer, works, but...
1) the trick was to change the id whenever you change the "cell" from favorite to non-favorite -> i assume Apple uses UITableView - logic with dequeueResubleCell and if the id is the same it won't be updated, instead just reused/copied and therefore the heart did not change
2) i change now the id randomly when favorites change - you have to think of a better/cleaner solution there.
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
}
}
}
Upvotes: 2
Reputation: 168
have you verified .onTapGesture
is being called? alternatively you could instead put self.toggleFavorite(business: favorite)
inside action of the button.
Button(
action: { self.toggleFavorite(business: business) },
label: { Image(systemName: "heart") }
)
Upvotes: 0