Home > Mobile >  How to get a row count in SwiftUI List
How to get a row count in SwiftUI List

Time:12-31

I am trying to display the number of rows in a section in its header as shown below as COUNTHERE. The issue I'm running into is that I can't put any code inside the if statement that is not a view so I can't compute anything. Ideas?

struct Day1View: View {

var displayEmployees: [Employee]

var body: some View {
    List {
        Section(header: Text("Early (\(COUNTHERE)")) {
            ForEach(displayEmployees) { employee in
                if employee.shift == .early {
                    switch employee.post {
                    case .kitchen : Text(employee.name).foregroundColor(.blue)
                    case .floor : Text(employee.name).foregroundColor(.yellow)
                    case .upstairs : Text(employee.name).foregroundColor(.red)
                    case .greeting : Text(employee.name).foregroundColor(.green)
                    default : Text(employee.name).foregroundColor(.gray)
                    }
                }
            }
        }
     }

CodePudding user response:

Since the section you showed is only for .early shift employees, you can get the count using a filtered version of the original array:

displayEmployees.filter({$0.shift == .early}).count

So your section becomes:

Section(header: Text("Early (\(displayEmployees.filter({$0.shift == .early}).count)")) {           

Or, you can add a new computed property for the count:

var displayCount: Int {
    return displayEmployees.filter({$0.shift == .early}).count
}
...
Section(header: Text("Early (\(displayCount)")) {

CodePudding user response:

To display the number of rows in the section header, you can use the @State property wrapper to store the count of the rows in the section and update it whenever the displayEmployees array changes.

Here's an example of how you can do this:

struct Day1View: View {
    var displayEmployees: [Employee]

    @State private var earlyShiftCount: Int = 0

    var body: some View {
        List {
            Section(header: Text("Early (\(earlyShiftCount)")) {
                ForEach(displayEmployees) { employee in
                    if employee.shift == .early {
                        switch employee.post {
                        case .kitchen : Text(employee.name).foregroundColor(.blue)
                        case .floor : Text(employee.name).foregroundColor(.yellow)
                        case .upstairs : Text(employee.name).foregroundColor(.red)
                        case .greeting : Text(employee.name).foregroundColor(.green)
                        default : Text(employee.name).foregroundColor(.gray)
                        }
                    }
                }
                .onChange(of: displayEmployees) { value in
                    self.earlyShiftCount = value.filter { $0.shift == .early }.count
                }
            }
        }
    }
}

In this example, the earlyShiftCount state variable is initialized to 0 and is updated whenever the displayEmployees array changes using the onChange modifier. The earlyShiftCount variable is then displayed in the section header using string interpolation.

I hope this helps! Let me know if you have any questions.

  • Related