UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Producing a list and removing gaps for omitted data

Forums > SwiftUI

I'm working on an app in SwiftUI, Xcode 12.4 / iOS14.

In one view I need to produce a list - but the data for the list is subject to a 'ForEach loop' and then an 'if / else' condition - if a condition is met then the item should be omitted from the list. This works - but the list now has gaps in it from where items have been omitted.

Question is - is there a way to produce a list and omit the gaps or would I need to rebuild a new set of data using the if / else condition and then pass that to the list to avoid gaps ?

2      

It would help if we could see some code.

But you probably want to use filter(_:) on your array before running it through the ForEach to get rid of items you don't want to display.

2      

The way I did it (because used a FetchRequest) was have a computed property that return the new array without the data in it

var newItems: [Data] {
    var items = [Data]()

    for item in orginalItems {
        if true {
          items.append(item)
        }
    }

    return items
}

then use newItems in the ForEach

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.