refactor code of password details

This commit is contained in:
Bob Sun 2017-02-04 20:38:40 +08:00
parent 2ecb7ae764
commit 2632169427
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4

View file

@ -11,43 +11,57 @@ import UIKit
class PasswordDetailTableViewController: UITableViewController { class PasswordDetailTableViewController: UITableViewController {
var passwordEntity: PasswordEntity? var passwordEntity: PasswordEntity?
struct FormCell { struct TableCell {
var title: String var title: String
var content: String var content: String
} }
var formData = [[FormCell]]() struct TableSection {
var title: String
var item: Array<TableCell>
}
var tableData = Array<TableSection>()
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell") tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
let password = passwordEntity!.decrypt()! let password = passwordEntity!.decrypt()!
formData.append([])
tableData.append(TableSection(title: "", item: []))
if let username = password.additions["Username"] { if let username = password.additions["Username"] {
formData[0].append(FormCell(title: "username", content: username)) tableData[0].item.append(TableCell(title: "username", content: username))
password.additions.removeValue(forKey: "Username")
}
tableData[0].item.append(TableCell(title: "password", content: password.password))
if password.additions.count > 0 {
tableData.append(TableSection(title: "additions", item: []))
} }
formData[0].append(FormCell(title: "password", content: password.password))
} }
override func numberOfSections(in tableView: UITableView) -> Int { override func numberOfSections(in tableView: UITableView) -> Int {
return formData.count return tableData.count
} }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return formData[section].count return tableData[section].item.count
} }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell
cell.titleLabel.text = formData[indexPath.section][indexPath.row].title cell.titleLabel.text = tableData[indexPath.section].item[indexPath.row].title
cell.contentLabel.text = formData[indexPath.section][indexPath.row].content cell.contentLabel.text = tableData[indexPath.section].item[indexPath.row].content
return cell return cell
} }
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return tableData[section].title
}
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) { override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
if action == #selector(copy(_:)) { if action == #selector(copy(_:)) {
UIPasteboard.general.string = formData[indexPath.section][indexPath.row].content UIPasteboard.general.string = tableData[indexPath.section].item[indexPath.row].content
} }
} }