From 26321694271d49b7b43f88713910393c35843f5a Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Sat, 4 Feb 2017 20:38:40 +0800 Subject: [PATCH] refactor code of password details --- pass/PasswordDetailTableViewController.swift | 36 ++++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/pass/PasswordDetailTableViewController.swift b/pass/PasswordDetailTableViewController.swift index b2b4763..abd064e 100644 --- a/pass/PasswordDetailTableViewController.swift +++ b/pass/PasswordDetailTableViewController.swift @@ -11,43 +11,57 @@ import UIKit class PasswordDetailTableViewController: UITableViewController { var passwordEntity: PasswordEntity? - struct FormCell { + struct TableCell { var title: String var content: String } - var formData = [[FormCell]]() + struct TableSection { + var title: String + var item: Array + } + + var tableData = Array() override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell") let password = passwordEntity!.decrypt()! - formData.append([]) + + tableData.append(TableSection(title: "", item: [])) 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 { - return formData.count + return tableData.count } 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 { let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell - cell.titleLabel.text = formData[indexPath.section][indexPath.row].title - cell.contentLabel.text = formData[indexPath.section][indexPath.row].content + cell.titleLabel.text = tableData[indexPath.section].item[indexPath.row].title + cell.contentLabel.text = tableData[indexPath.section].item[indexPath.row].content 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?) { if action == #selector(copy(_:)) { - UIPasteboard.general.string = formData[indexPath.section][indexPath.row].content + UIPasteboard.general.string = tableData[indexPath.section].item[indexPath.row].content } }