Skip to content

Instantly share code, notes, and snippets.

@ramiresnas
Created January 25, 2018 18:30
Show Gist options
  • Save ramiresnas/ac3d2ff1e3b7ef1fb6c035fc3426ec38 to your computer and use it in GitHub Desktop.
Save ramiresnas/ac3d2ff1e3b7ef1fb6c035fc3426ec38 to your computer and use it in GitHub Desktop.
table view com opções
//
// ConfigurationViewController.swift
// odonto
//
// Created by Ramires Moreira on 1/15/18.
// Copyright © 2018 Ramires Moreira. All rights reserved.
//
import UIKit
class ConfigurationViewController: UIViewController , UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var table : UITableView!
let headers = ["Configurações de agendamento","Perfil"]
let cells : [[String : Option]] = [["adcionar ao calendario" : .addToCalendar ,
"lembrar hora de sair" : .remindMe ],
["sair" : .sair]]
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cells[section].count
}
func numberOfSections(in tableView: UITableView) -> Int {
return headers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "simpleCell", for: indexPath)
let descricao = cells[indexPath.section].keys.filter {s in true}[indexPath.row]
if indexPath.section == 0{
let option = cells[indexPath.section].values.filter {o in true}[indexPath.row]
let cellToReturn = MyCell(cell, descricao, option: option)
return cellToReturn.cell
}
cell.textLabel?.text = descricao
return cell
}
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return headers[section]
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
table.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 && indexPath.section == 1{
LoginViewController.logOut()
}
}
}
enum Option{
case sair
case addToCalendar
case remindMe
}
class MyCell {
let cell : UITableViewCell
init( _ reusableCell : UITableViewCell, _ descricao : String , option : Option) {
cell = reusableCell
cell.textLabel?.text = descricao
let rect = CGRect(x: 0, y: 10, width: 60 , height: 30)
let swap = UISwitch(frame: rect)
swap.addTarget(self, action: #selector(preferencesChange), for: UIControlEvents.valueChanged)
cell.accessoryView = swap
}
@objc func preferencesChange( ) {
print("mudou")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment