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()
}
}
swift uitableview
add a comment |
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()
}
}
swift uitableview
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
add a comment |
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()
}
}
swift uitableview
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
swift uitableview
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
add a comment |
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
add a comment |
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
}
}
add a comment |
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
}
}
add a comment |
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
}
}
add a comment |
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
}
}
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
}
}
answered Nov 11 at 16:59
regina_fallangi
838723
838723
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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