SwiftUI 列表未使用 ForEach 正确更新

2024-04-27

我正在尝试使用带有两个 ForEach 循环的列表创建收藏夹功能。当我单击按钮时,该项目会移动到正确的部分,但按钮图像和功能不会更新。我是否遗漏或做错了什么?

如果我导航到另一个视图并返回,列表最初会正确呈现,但如果我单击按钮,仍然会表现出相同的行为。

如果我使用两个具有完全相同设置的单独列表,则一切正常,但在 UI 中看起来很奇怪,因为即使列表中没有项目,每个列表也会占用相同的空间。

或者,有没有办法强制列表作为按钮点击的一部分重新绘制?

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
        }
    }
}

更新的答案,有效,但是......

1)诀窍是每当你将“单元格”从最喜欢的更改为非最喜欢的时更改id - >我假设Apple使用UITableView - 具有dequeueResubleCell的逻辑,如果id相同,则不会更新,而只是重用/复制了所以心没变

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
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SwiftUI 列表未使用 ForEach 正确更新 的相关文章

随机推荐