passforios/pass/PasswordDetailTableViewController.swift

81 lines
2.8 KiB
Swift
Raw Normal View History

2017-02-02 21:04:31 +08:00
//
// PasswordDetailTableViewController.swift
// pass
//
// Created by Mingshen Sun on 2/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
class PasswordDetailTableViewController: UITableViewController {
var passwordEntity: PasswordEntity?
2017-02-04 20:38:40 +08:00
struct TableCell {
2017-02-02 21:04:31 +08:00
var title: String
var content: String
}
2017-02-04 20:38:40 +08:00
struct TableSection {
var title: String
var item: Array<TableCell>
}
var tableData = Array<TableSection>()
2017-02-02 21:04:31 +08:00
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
let password = passwordEntity!.decrypt()!
2017-02-04 20:38:40 +08:00
tableData.append(TableSection(title: "", item: []))
2017-02-02 21:04:31 +08:00
if let username = password.additions["Username"] {
2017-02-04 20:38:40 +08:00
tableData[0].item.append(TableCell(title: "username", content: username))
password.additions.removeValue(forKey: "Username")
2017-02-02 21:04:31 +08:00
}
2017-02-04 20:38:40 +08:00
tableData[0].item.append(TableCell(title: "password", content: password.password))
2017-02-02 21:04:31 +08:00
2017-02-04 20:38:40 +08:00
if password.additions.count > 0 {
tableData.append(TableSection(title: "additions", item: []))
}
}
2017-02-02 21:04:31 +08:00
override func numberOfSections(in tableView: UITableView) -> Int {
2017-02-04 20:38:40 +08:00
return tableData.count
2017-02-02 21:04:31 +08:00
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
2017-02-04 20:38:40 +08:00
return tableData[section].item.count
2017-02-02 21:04:31 +08:00
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell
2017-02-04 20:38:40 +08:00
cell.titleLabel.text = tableData[indexPath.section].item[indexPath.row].title
cell.contentLabel.text = tableData[indexPath.section].item[indexPath.row].content
2017-02-02 21:04:31 +08:00
return cell
}
2017-02-04 20:38:40 +08:00
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return tableData[section].title
}
2017-02-03 18:02:40 +08:00
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
if action == #selector(copy(_:)) {
2017-02-04 20:38:40 +08:00
UIPasteboard.general.string = tableData[indexPath.section].item[indexPath.row].content
2017-02-03 18:02:40 +08:00
}
}
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return action == #selector(UIResponderStandardEditActions.copy(_:))
}
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
2017-02-02 21:04:31 +08:00
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 52
}
}