Created
August 24, 2018 11:23
-
-
Save goergisn/3abbe4dfeadc51de515312164708c259 to your computer and use it in GitHub Desktop.
Using enums to build a reliable section model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import MessageUI | |
class SimpleSectionsViewController: UITableViewController { | |
enum UserInfoRow { | |
case name | |
} | |
enum ContactInfoRow { | |
case email | |
case github | |
} | |
enum Section { | |
case userInfo(_ rows:[UserInfoRow]) | |
case contactInfo(_ rows:[ContactInfoRow]) | |
} | |
private var sections:[Section] = [] | |
// MARK: - UITableViewController | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell.init(style: .subtitle, reuseIdentifier: "Cell") | |
switch sections[indexPath.section] { | |
case .contactInfo(let rows): | |
switch rows[indexPath.row] { | |
case .email: | |
cell.textLabel?.text = "Email" | |
cell.detailTextLabel?.text = "[email protected]" | |
cell.selectionStyle = .none | |
case .github: | |
cell.textLabel?.text = "Github" | |
cell.detailTextLabel?.text = "github.com/goergisn" | |
} | |
case .userInfo(let rows): | |
switch rows[indexPath.row] { | |
case .name: | |
cell.textLabel?.text = "Name" | |
cell.detailTextLabel?.text = "Alex Guretzki" | |
cell.selectionStyle = .none | |
} | |
} | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
switch sections[section] { | |
case .contactInfo(_): return "Contact" | |
case .userInfo(_): return "User" | |
} | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
tableView.deselectRow(at: indexPath, animated: true) | |
switch sections[indexPath.section] { | |
case .contactInfo(let rows): | |
switch rows[indexPath.row] { | |
case .email: | |
if MFMailComposeViewController.canSendMail() { | |
let viewController = MFMailComposeViewController() | |
viewController.setToRecipients(["[email protected]"]) | |
viewController.setSubject("Hello!") | |
viewController.setMessageBody("Great code!", isHTML: false) | |
self.present(viewController, animated: true, completion: nil) | |
} else { | |
print("Y u no have mail?") | |
} | |
case .github: | |
if let url = URL.init(string: "https://github.com/goergisn") { | |
UIApplication.shared.open(url, completionHandler: nil) | |
} | |
} | |
case .userInfo(let rows): | |
switch rows[indexPath.row] { | |
case .name: | |
print("Can't touch this!") | |
} | |
} | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
switch sections[section] { | |
case .contactInfo(let rows): return rows.count | |
case .userInfo(let rows): return rows.count | |
} | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return sections.count | |
} | |
// MARK: - UIViewController | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
sections = [.userInfo([.name]), | |
.contactInfo([.email, .github])] | |
self.title = "Simple Sections" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment