SwiftUI列表未使用ForEach正确更新

时间:2020-05-27 16:39:46

标签: swiftui-list swiftui

我正在尝试使用具有两个ForEach循环的List创建收藏夹功能。当我单击按钮时,该项目将移至正确的部分,但按钮的图像和功能不会更新。我有什么想念的东西吗?

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

如果我使用两个具有完全相同设置的单独列表,则一切正常,但是在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
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您已验证正在呼叫.onTapGesture吗?或者,您也可以将self.toggleFavorite(business: favorite)放在按钮的动作中。

                            Button(
                                action: { self.toggleFavorite(business: business) },
                                label: { Image(systemName: "heart") }
                            )

答案 1 :(得分:0)

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

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

Result