Determine enum case with section











up vote
1
down vote

favorite












I am implementing a UITableView which will have multiple sections. The amount of sections are dynamic. This are some properties:



public struct User { 
let userRole: UserRole
}

public enum UserRole: Int, CaseIterable {
case superUser, admin, recruiter
}


My UITableView holds an array of users. The amount of sections are the amount of distinct userRoles in the array of users. Later on, I need to determine how many rows there are in each section with this function:



public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}


I am wondering what the most clean/fastest way is to return the amount of rows in that section. Imagine the user array holds 2 users, one has a userRole of superUser and the other recruiter. The sections are 0 and 1, although the enum raw values are 0 and 2.



I have no idea how to return the correct amount of rows in that section in a clean way. This is what I was writing, but stopped since I though there must be a better way:



extension Array where Element: User {
func contains(userRole: UserRole) -> Bool {
return contains(where: { $0.userRole == userRole })
}
}

private func determineUserRoleForSection(section: Int) -> UserRole {
let containsSuperUser = users.contains(userRole: .superUser)
let containsAdmin = users.contains(userRole: .admin)
let containsRecruiter = users.contains(userRole: .recruiter)

switch section {
case 0:
if containsSuperUser {
return .superUser
}

if containsAdmin {
return .admin
}

if containsRecruiter {
return .recruiter
}
case 1:
// Repeat without superUser case
case 2:
default: fatalError()
}
}









share|improve this question


















  • 1




    You need a proper data model for the table view. When you have multiple sections in a table view you essentially want an array of arrays, not a single array.
    – rmaddy
    Nov 11 at 16:46










  • @rmaddy Hmm thanks for the tip, never thought about that.
    – J. Doe
    Nov 11 at 16:48















up vote
1
down vote

favorite












I am implementing a UITableView which will have multiple sections. The amount of sections are dynamic. This are some properties:



public struct User { 
let userRole: UserRole
}

public enum UserRole: Int, CaseIterable {
case superUser, admin, recruiter
}


My UITableView holds an array of users. The amount of sections are the amount of distinct userRoles in the array of users. Later on, I need to determine how many rows there are in each section with this function:



public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}


I am wondering what the most clean/fastest way is to return the amount of rows in that section. Imagine the user array holds 2 users, one has a userRole of superUser and the other recruiter. The sections are 0 and 1, although the enum raw values are 0 and 2.



I have no idea how to return the correct amount of rows in that section in a clean way. This is what I was writing, but stopped since I though there must be a better way:



extension Array where Element: User {
func contains(userRole: UserRole) -> Bool {
return contains(where: { $0.userRole == userRole })
}
}

private func determineUserRoleForSection(section: Int) -> UserRole {
let containsSuperUser = users.contains(userRole: .superUser)
let containsAdmin = users.contains(userRole: .admin)
let containsRecruiter = users.contains(userRole: .recruiter)

switch section {
case 0:
if containsSuperUser {
return .superUser
}

if containsAdmin {
return .admin
}

if containsRecruiter {
return .recruiter
}
case 1:
// Repeat without superUser case
case 2:
default: fatalError()
}
}









share|improve this question


















  • 1




    You need a proper data model for the table view. When you have multiple sections in a table view you essentially want an array of arrays, not a single array.
    – rmaddy
    Nov 11 at 16:46










  • @rmaddy Hmm thanks for the tip, never thought about that.
    – J. Doe
    Nov 11 at 16:48













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am implementing a UITableView which will have multiple sections. The amount of sections are dynamic. This are some properties:



public struct User { 
let userRole: UserRole
}

public enum UserRole: Int, CaseIterable {
case superUser, admin, recruiter
}


My UITableView holds an array of users. The amount of sections are the amount of distinct userRoles in the array of users. Later on, I need to determine how many rows there are in each section with this function:



public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}


I am wondering what the most clean/fastest way is to return the amount of rows in that section. Imagine the user array holds 2 users, one has a userRole of superUser and the other recruiter. The sections are 0 and 1, although the enum raw values are 0 and 2.



I have no idea how to return the correct amount of rows in that section in a clean way. This is what I was writing, but stopped since I though there must be a better way:



extension Array where Element: User {
func contains(userRole: UserRole) -> Bool {
return contains(where: { $0.userRole == userRole })
}
}

private func determineUserRoleForSection(section: Int) -> UserRole {
let containsSuperUser = users.contains(userRole: .superUser)
let containsAdmin = users.contains(userRole: .admin)
let containsRecruiter = users.contains(userRole: .recruiter)

switch section {
case 0:
if containsSuperUser {
return .superUser
}

if containsAdmin {
return .admin
}

if containsRecruiter {
return .recruiter
}
case 1:
// Repeat without superUser case
case 2:
default: fatalError()
}
}









share|improve this question













I am implementing a UITableView which will have multiple sections. The amount of sections are dynamic. This are some properties:



public struct User { 
let userRole: UserRole
}

public enum UserRole: Int, CaseIterable {
case superUser, admin, recruiter
}


My UITableView holds an array of users. The amount of sections are the amount of distinct userRoles in the array of users. Later on, I need to determine how many rows there are in each section with this function:



public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}


I am wondering what the most clean/fastest way is to return the amount of rows in that section. Imagine the user array holds 2 users, one has a userRole of superUser and the other recruiter. The sections are 0 and 1, although the enum raw values are 0 and 2.



I have no idea how to return the correct amount of rows in that section in a clean way. This is what I was writing, but stopped since I though there must be a better way:



extension Array where Element: User {
func contains(userRole: UserRole) -> Bool {
return contains(where: { $0.userRole == userRole })
}
}

private func determineUserRoleForSection(section: Int) -> UserRole {
let containsSuperUser = users.contains(userRole: .superUser)
let containsAdmin = users.contains(userRole: .admin)
let containsRecruiter = users.contains(userRole: .recruiter)

switch section {
case 0:
if containsSuperUser {
return .superUser
}

if containsAdmin {
return .admin
}

if containsRecruiter {
return .recruiter
}
case 1:
// Repeat without superUser case
case 2:
default: fatalError()
}
}






swift uitableview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 16:43









J. Doe

2,6911932




2,6911932








  • 1




    You need a proper data model for the table view. When you have multiple sections in a table view you essentially want an array of arrays, not a single array.
    – rmaddy
    Nov 11 at 16:46










  • @rmaddy Hmm thanks for the tip, never thought about that.
    – J. Doe
    Nov 11 at 16:48














  • 1




    You need a proper data model for the table view. When you have multiple sections in a table view you essentially want an array of arrays, not a single array.
    – rmaddy
    Nov 11 at 16:46










  • @rmaddy Hmm thanks for the tip, never thought about that.
    – J. Doe
    Nov 11 at 16:48








1




1




You need a proper data model for the table view. When you have multiple sections in a table view you essentially want an array of arrays, not a single array.
– rmaddy
Nov 11 at 16:46




You need a proper data model for the table view. When you have multiple sections in a table view you essentially want an array of arrays, not a single array.
– rmaddy
Nov 11 at 16:46












@rmaddy Hmm thanks for the tip, never thought about that.
– J. Doe
Nov 11 at 16:48




@rmaddy Hmm thanks for the tip, never thought about that.
– J. Doe
Nov 11 at 16:48












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Following the recommendation of having a model, you could do something like this:



public struct UsersViewModel {
var users: [User]
private var sections: [UserRole: [User]] {
return [
.admin : users.filter { $0.userRole == .admin },
.superUser : users.filter { $0.userRole == .superUser },
.recruiter : users.filter { $0.userRole == .recruiter }
]
}
var superUserSections: Int { return sections[.superUser]!.count}
var adminSections: Int { return sections[.admin]!.count }
var recruiterSections: Int { return sections[.recruiter]!.count }
}


And then it'd just be a matter of using:



public func tableView(_ tableView: UITableView, numberOfSections section: Int) -> Int {
return 3 // section 0 is superUser, section 1 is admin, section 2 is recruiter.
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return viewModel.superUserSections
case 1:
return viewModel.adminSections
case 2:
return viewModel.recruiterSections
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250914%2fdetermine-enum-case-with-section%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Following the recommendation of having a model, you could do something like this:



    public struct UsersViewModel {
    var users: [User]
    private var sections: [UserRole: [User]] {
    return [
    .admin : users.filter { $0.userRole == .admin },
    .superUser : users.filter { $0.userRole == .superUser },
    .recruiter : users.filter { $0.userRole == .recruiter }
    ]
    }
    var superUserSections: Int { return sections[.superUser]!.count}
    var adminSections: Int { return sections[.admin]!.count }
    var recruiterSections: Int { return sections[.recruiter]!.count }
    }


    And then it'd just be a matter of using:



    public func tableView(_ tableView: UITableView, numberOfSections section: Int) -> Int {
    return 3 // section 0 is superUser, section 1 is admin, section 2 is recruiter.
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
    return viewModel.superUserSections
    case 1:
    return viewModel.adminSections
    case 2:
    return viewModel.recruiterSections
    }
    }





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Following the recommendation of having a model, you could do something like this:



      public struct UsersViewModel {
      var users: [User]
      private var sections: [UserRole: [User]] {
      return [
      .admin : users.filter { $0.userRole == .admin },
      .superUser : users.filter { $0.userRole == .superUser },
      .recruiter : users.filter { $0.userRole == .recruiter }
      ]
      }
      var superUserSections: Int { return sections[.superUser]!.count}
      var adminSections: Int { return sections[.admin]!.count }
      var recruiterSections: Int { return sections[.recruiter]!.count }
      }


      And then it'd just be a matter of using:



      public func tableView(_ tableView: UITableView, numberOfSections section: Int) -> Int {
      return 3 // section 0 is superUser, section 1 is admin, section 2 is recruiter.
      }

      public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      switch section {
      case 0:
      return viewModel.superUserSections
      case 1:
      return viewModel.adminSections
      case 2:
      return viewModel.recruiterSections
      }
      }





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Following the recommendation of having a model, you could do something like this:



        public struct UsersViewModel {
        var users: [User]
        private var sections: [UserRole: [User]] {
        return [
        .admin : users.filter { $0.userRole == .admin },
        .superUser : users.filter { $0.userRole == .superUser },
        .recruiter : users.filter { $0.userRole == .recruiter }
        ]
        }
        var superUserSections: Int { return sections[.superUser]!.count}
        var adminSections: Int { return sections[.admin]!.count }
        var recruiterSections: Int { return sections[.recruiter]!.count }
        }


        And then it'd just be a matter of using:



        public func tableView(_ tableView: UITableView, numberOfSections section: Int) -> Int {
        return 3 // section 0 is superUser, section 1 is admin, section 2 is recruiter.
        }

        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
        return viewModel.superUserSections
        case 1:
        return viewModel.adminSections
        case 2:
        return viewModel.recruiterSections
        }
        }





        share|improve this answer












        Following the recommendation of having a model, you could do something like this:



        public struct UsersViewModel {
        var users: [User]
        private var sections: [UserRole: [User]] {
        return [
        .admin : users.filter { $0.userRole == .admin },
        .superUser : users.filter { $0.userRole == .superUser },
        .recruiter : users.filter { $0.userRole == .recruiter }
        ]
        }
        var superUserSections: Int { return sections[.superUser]!.count}
        var adminSections: Int { return sections[.admin]!.count }
        var recruiterSections: Int { return sections[.recruiter]!.count }
        }


        And then it'd just be a matter of using:



        public func tableView(_ tableView: UITableView, numberOfSections section: Int) -> Int {
        return 3 // section 0 is superUser, section 1 is admin, section 2 is recruiter.
        }

        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
        return viewModel.superUserSections
        case 1:
        return viewModel.adminSections
        case 2:
        return viewModel.recruiterSections
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 16:59









        regina_fallangi

        838723




        838723






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250914%2fdetermine-enum-case-with-section%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine