passforios/pass/Controllers/AddPasswordTableViewController.swift

82 lines
3.3 KiB
Swift
Raw Normal View History

2017-02-10 22:15:01 +08:00
//
// AddPasswordTableViewController.swift
// pass
//
// Created by Mingshen Sun on 10/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
class AddPasswordTableViewController: UITableViewController {
let tableTitles = ["name", "password", "additions"]
2017-02-10 22:15:01 +08:00
var password: Password?
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell")
tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell")
tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell")
2017-02-10 22:15:01 +08:00
tableView.rowHeight = UITableViewAutomaticDimension
2017-02-11 20:45:56 +08:00
tableView.estimatedRowHeight = 48
2017-02-10 22:15:01 +08:00
tableView.allowsSelection = false
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
2017-02-11 20:45:56 +08:00
return 1
}
override func numberOfSections(in tableView: UITableView) -> Int {
2017-02-10 22:15:01 +08:00
return tableTitles.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch tableTitles[indexPath.section] {
case "additions":
let cell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as! TextViewTableViewCell
cell.contentTextView.text = ""
return cell
case "password":
let cell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as! FillPasswordTableViewCell
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! TextFieldTableViewCell
2017-02-11 20:49:21 +08:00
cell.contentTextField.placeholder = tableTitles[indexPath.section]
return cell
}
2017-02-10 22:15:01 +08:00
}
2017-02-11 20:45:56 +08:00
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UITableViewHeaderFooterView()
headerView.textLabel?.text = tableTitles[section].uppercased()
return headerView
}
2017-02-10 22:15:01 +08:00
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
2017-02-11 16:12:10 +08:00
if segue.identifier == "saveAddPasswordSegue" {
let nameCell = getCellForName(name: "name")! as! TextFieldTableViewCell
let passwordCell = getCellForName(name: "password")! as! FillPasswordTableViewCell
2017-02-11 16:12:10 +08:00
let additionsCell = getCellForName(name: "additions")! as! TextViewTableViewCell
password = Password(name: nameCell.contentTextField.text!, plainText: "\(passwordCell.contentTextField.text!)\n\(additionsCell.contentTextView.text!)")
}
2017-02-10 22:15:01 +08:00
}
2017-02-11 20:45:56 +08:00
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
2017-02-10 22:15:01 +08:00
2017-02-11 20:45:56 +08:00
func getCellAt(section: Int) -> UITableViewCell? {
return tableView.cellForRow(at: IndexPath(row: 0, section: section))
2017-02-10 22:15:01 +08:00
}
func getCellForName(name: String) -> UITableViewCell? {
2017-02-10 22:15:01 +08:00
let index = tableTitles.index(of: name)!
2017-02-11 20:45:56 +08:00
return getCellAt(section: Int(index))
2017-02-10 22:15:01 +08:00
}
}