Lifeplus
Lifeplus

Reputation: 571

How to hide empty rows in List

I have created simple List, but I want to hide empty rows. Any help? I know how to hide them for UITableView but not for new SwiftUI List. I tried documentation, but I didn't find anything.

struct LandmarkList: View {
    @EnvironmentObject var userData: UserData

    var body: some View {
        NavigationView {
            List {
                Toggle(isOn: $userData.showFavoritesOnly) {
                    Text("Favorites only")
                }
                ForEach(userData.landmarks) { landmark in
                    if !self.userData.showFavoritesOnly || landmark.isFavorite {
                        NavigationButton(destination: LandmarkDetail(landmark: landmark)) {
                            LandmarkRow(landmark: landmark)
                        }
                    }
                }
            }
            .navigationBarTitle(Text("Landmarks"), displayMode: .large)
        }
    }
}

Result

Result

Upvotes: 3

Views: 1610

Answers (2)

Lifeplus
Lifeplus

Reputation: 571

Probably best solution for now is to create ScrollView and create rows with ForEach in it.

Upvotes: 1

sachin jeph
sachin jeph

Reputation: 209

Not exactly a solution to this problem but one way of getting rid of the lines in the list is by using the modifier

List{
// Different Views 
}.listStyle(.grouped)

Upvotes: 1

Related Questions