This file contains hidden or 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
override func viewDidLoad() { | |
super.viewDidLoad() | |
// 取讀螢幕尺寸 | |
let fullScreenSize = UIScreen.main.bounds.size | |
// 設定UITableView,原點/尺寸,另外style可設定是否tableView是一大串row或是有section分隔 | |
let myTableView = UITableView(frame: CGRect(x: 0, y: 20, width: fullScreenSize.width, height: fullScreenSize.height - 20), style: .grouped) | |
// 註冊cell | |
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") | |
// 設定Delegate/DataSource對象(也就是這一個ViewController本身) | |
myTableView.delegate = self |
This file contains hidden or 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
extension ViewController: UITableViewDataSource { | |
// 顯示每一組有幾個cell | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return info[section].count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell | |
if indexPath.section == 1 { | |
if indexPath.row == 0 { |
This file contains hidden or 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
extension ViewController: UITableViewDelegate { | |
// 點選cell後執行的動作 | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
// 設置取消點選cell後的動畫 | |
tableView.deselectRow(at: indexPath, animated: true) | |
let name = info[indexPath.section][indexPath.row] | |
print("選擇的是\(name)") | |
} | |
/* | |
點選 Accessory 按鈕後執行的動作 |
This file contains hidden or 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
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) { | |
var textField = UITextField() | |
let controller = UIAlertController(title: "Add New Item", message: "", preferredStyle: .alert) | |
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in | |
do { | |
try self.realm.write { | |
let newItem = Item() | |
newItem.title = textField.text! | |
newItem.order += 1 | |
self.realm.add(newItem) |
This file contains hidden or 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
extension TodoItemViewController: SwipeTableViewCellDelegate { | |
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? { | |
guard orientation == .right else { return nil } | |
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in | |
if let item = self.todoItems?[indexPath.row] { | |
do { | |
try self.realm.write { | |
self.realm.delete(item) | |
} |
This file contains hidden or 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
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { | |
do { | |
try realm.write { | |
let sourceItem = todoItems?[sourceIndexPath.row] | |
let destinationItem = todoItems?[destinationIndexPath.row] | |
print(sourceIndexPath.row) | |
let destinationItemOrder = destinationItem?.order | |
if sourceIndexPath.row < destinationIndexPath.row { | |
// 由上往下移動 |
This file contains hidden or 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
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SwipeTableViewCell | |
cell.delegate = self | |
// 因為todoItems是optional,用optional binding來確保item型別不是optional | |
if let item = todoItems?[indexPath.row] { | |
cell.textLabel?.text = item.title | |
cell.accessoryType = item.done ? .checkmark : .none | |
} else { | |
cell.textLabel?.text = "No Items Added" | |
} |
This file contains hidden or 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
@IBAction func signUpPressed(_ sender: UIButton) { | |
// Validate the field, 先對textField做判斷 | |
let error = validateTextField() | |
if error != nil { | |
popAlert(title: "Error occured", message: error!, alertTitle: "OK") { (action) in | |
self.emailTextField.text = "" | |
self.passwordTextField.text = "" | |
self.repasswordTextField.text = "" | |
self.userNameTextField.text = "" | |
} |
This file contains hidden or 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
@IBAction func loginPressed(_ sender: UIButton) { | |
let error = validateTextField() | |
if error != nil { | |
popAlert(title: "Error occured", message: error!, alertTitle: "OK") { (action) in | |
self.emailTextField.text = "" | |
self.passwordTextField.text = "" | |
} | |
} else { | |
if let email = emailTextField.text, | |
let password = passwordTextField.text { |
This file contains hidden or 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
@IBAction func logoutPressed(_ sender: UIButton) { | |
// 畫面顯示怪怪的,再確認 | |
do { | |
try Auth.auth().signOut() | |
// navigationController?.popToRootViewController(animated: true) | |
var rootVC = self.presentingViewController | |
while let parent = rootVC?.presentingViewController { | |
rootVC = parent | |
} | |
rootVC?.dismiss(animated: true, completion: nil) |