update basictableviewcontroller

This commit is contained in:
Bob Sun 2017-02-10 16:44:59 +08:00
parent 24139e932b
commit 2487b23a10
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
7 changed files with 46 additions and 28 deletions

View file

@ -40,9 +40,9 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController {
let formatted = ByteCountFormatter.string(fromByteCount: Int64(size), countStyle: ByteCountFormatter.CountStyle.file) let formatted = ByteCountFormatter.string(fromByteCount: Int64(size), countStyle: ByteCountFormatter.CountStyle.file)
self?.tableData = [ self?.tableData = [
// section 0 // section 0
[[.type: CellDataType.detail, .title: "Passwords", .detailText: String(passwordEntities.count)], [[.style: CellDataStyle.value1, .accessoryType: UITableViewCellAccessoryType.none, .title: "Passwords", .detailText: String(passwordEntities.count)],
[.type: CellDataType.detail, .title: "Size", .detailText: formatted], [.style: CellDataStyle.value1, .accessoryType: UITableViewCellAccessoryType.none, .title: "Size", .detailText: formatted],
[.type: CellDataType.detail, .title: "Last Synced", .detailText: Utils.getLastUpdatedTimeString()], [.style: CellDataStyle.value1, .accessoryType: UITableViewCellAccessoryType.none, .title: "Last Synced", .detailText: Utils.getLastUpdatedTimeString()],
], ],
] ]
indicator.stopAnimating() indicator.stopAnimating()

View file

@ -13,12 +13,12 @@ class AboutTableViewController: BasicStaticTableViewController {
override func viewDidLoad() { override func viewDidLoad() {
tableData = [ tableData = [
// section 0 // section 0
[[.type: CellDataType.link, .title: "Website", .link: "https://github.com/mssun/pass-ios.git"], [[.title: "Website", .action: "link", .link: "https://github.com/mssun/pass-ios.git"],
[.type: CellDataType.link, .title: "Contact Developer", .link: "mailto:bob@mssun.me?subject=passforiOS"],], [.title: "Contact Developer", .action: "link", .link: "mailto:bob@mssun.me?subject=passforiOS"],],
// section 1, // section 1,
[[.type: CellDataType.segue, .title: "Open Source Components", .link: "showOpenSourceComponentsSegue"], [[.title: "Open Source Components", .action: "segue", .link: "showOpenSourceComponentsSegue"],
[.type: CellDataType.segue, .title: "Special Thanks", .link: "showSpecialThanksSegue"],], [.title: "Special Thanks", .action: "segue", .link: "showSpecialThanksSegue"],],
] ]
navigationItemTitle = "About" navigationItemTitle = "About"
super.viewDidLoad() super.viewDidLoad()

View file

@ -15,8 +15,12 @@ enum CellDataType {
case link, segue, empty, detail case link, segue, empty, detail
} }
enum CellDataStyle {
case value1, defaultStyle
}
enum CellDataKey { enum CellDataKey {
case type, title, link, accessoryType, detailDisclosureAction, detailDisclosureData, detailText case style, type, title, link, accessoryType, detailDisclosureAction, detailDisclosureData, detailText, action
} }
class BasicStaticTableViewController: UITableViewController, MFMailComposeViewControllerDelegate { class BasicStaticTableViewController: UITableViewController, MFMailComposeViewControllerDelegate {
@ -44,22 +48,25 @@ class BasicStaticTableViewController: UITableViewController, MFMailComposeViewCo
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellData = tableData[indexPath.section][indexPath.row] let cellData = tableData[indexPath.section][indexPath.row]
let cellDataType = cellData[CellDataKey.type] as? CellDataType let cellDataStyle = cellData[CellDataKey.style] as? CellDataStyle
var cell: UITableViewCell? var cell: UITableViewCell?
switch cellDataType! {
case .detail: switch cellDataStyle ?? .defaultStyle {
case .value1:
cell = UITableViewCell(style: .value1, reuseIdentifier: "value1 cell") cell = UITableViewCell(style: .value1, reuseIdentifier: "value1 cell")
cell?.accessoryType = .none
cell?.detailTextLabel?.text = cellData[CellDataKey.detailText] as? String
default: default:
cell = UITableViewCell(style: .default, reuseIdentifier: "default cell") cell = UITableViewCell(style: .default, reuseIdentifier: "default cell")
cell?.textLabel?.text = cellData[CellDataKey.title] as? String
if let accessoryType = cellData[CellDataKey.accessoryType] as? UITableViewCellAccessoryType {
cell?.accessoryType = accessoryType
} else {
cell?.accessoryType = .disclosureIndicator
}
} }
if let detailText = cellData[CellDataKey.detailText] as? String {
cell?.detailTextLabel?.text = detailText
}
if let accessoryType = cellData[CellDataKey.accessoryType] as? UITableViewCellAccessoryType {
cell?.accessoryType = accessoryType
} else {
cell?.accessoryType = .disclosureIndicator
}
cell?.textLabel?.text = cellData[CellDataKey.title] as? String cell?.textLabel?.text = cellData[CellDataKey.title] as? String
return cell ?? UITableViewCell() return cell ?? UITableViewCell()
} }
@ -73,12 +80,12 @@ class BasicStaticTableViewController: UITableViewController, MFMailComposeViewCo
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true) tableView.deselectRow(at: indexPath, animated: true)
let cellData = tableData[indexPath.section][indexPath.row] let cellData = tableData[indexPath.section][indexPath.row]
let cellDataType = cellData[CellDataKey.type] as? CellDataType let cellDataAction = cellData[CellDataKey.action] as? String
switch cellDataType! { switch cellDataAction ?? "" {
case .segue: case "segue":
let link = cellData[CellDataKey.link] as? String let link = cellData[CellDataKey.link] as? String
performSegue(withIdentifier: link!, sender: self) performSegue(withIdentifier: link!, sender: self)
case .link: case "link":
let link = cellData[CellDataKey.link] as! String let link = cellData[CellDataKey.link] as! String
let url = URL(string: link)! let url = URL(string: link)!
switch url.scheme! { switch url.scheme! {

View file

@ -14,7 +14,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
navigationItemTitle = "General" navigationItemTitle = "General"
tableData = [ tableData = [
// section 0 // section 0
[[.type: CellDataType.segue, .title: "About Repository", .link: "showAboutRepositorySegue"],], [[.title: "About Repository", .action: "segue", .link: "showAboutRepositorySegue"],],
] ]
super.viewDidLoad() super.viewDidLoad()
} }

View file

@ -35,7 +35,7 @@ class OpenSourceComponentsTableViewController: BasicStaticTableViewController {
tableData.append([]) tableData.append([])
for item in openSourceComponents { for item in openSourceComponents {
tableData[0].append( tableData[0].append(
[CellDataKey.type: CellDataType.link, CellDataKey.title: item[0], CellDataKey.link: item[1], CellDataKey.accessoryType: UITableViewCellAccessoryType.detailDisclosureButton, CellDataKey.detailDisclosureAction: #selector(actOnDetailDisclosureButton(_:)), CellDataKey.detailDisclosureData: item[2]] [CellDataKey.title: item[0], CellDataKey.action: "link", CellDataKey.link: item[1], CellDataKey.accessoryType: UITableViewCellAccessoryType.detailDisclosureButton, CellDataKey.detailDisclosureAction: #selector(actOnDetailDisclosureButton(_:)), CellDataKey.detailDisclosureData: item[2]]
) )
} }
navigationItemTitle = "Open Source Components" navigationItemTitle = "Open Source Components"

View file

@ -12,14 +12,25 @@ import SVProgressHUD
class PasswordRepositorySettingsTableViewController: BasicStaticTableViewController { class PasswordRepositorySettingsTableViewController: BasicStaticTableViewController {
override func viewDidLoad() { override func viewDidLoad() {
let url = Defaults[.gitRepositoryURL]?.host
tableData = [ tableData = [
[[.type: CellDataType.segue, .title: "Git Server", .link: "showGitServerSettingSegue"], [[.style: CellDataStyle.value1, .title: "Git Server", .action: "segue", .link: "showGitServerSettingSegue", .detailText: url ?? ""],
[.type: CellDataType.segue, .title: "SSH Key", .link: "showSSHKeySettingSegue"],], [.title: "SSH Key", .action: "segue", .link: "showSSHKeySettingSegue"],],
] ]
navigationItemTitle = "Repository" navigationItemTitle = "Repository"
super.viewDidLoad() super.viewDidLoad()
} }
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let url = Defaults[.gitRepositoryURL] {
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
cell.detailTextLabel!.text = url.host
}
}
}
@IBAction func cancelGitServerSetting(segue: UIStoryboardSegue) { @IBAction func cancelGitServerSetting(segue: UIStoryboardSegue) {
} }

View file

@ -20,7 +20,7 @@ class SpecialThanksTableViewController: BasicStaticTableViewController {
tableData.append([]) tableData.append([])
for item in openSourceComponents { for item in openSourceComponents {
tableData[0].append( tableData[0].append(
[CellDataKey.type: CellDataType.link, CellDataKey.title: item[0], CellDataKey.link: item[1]] [CellDataKey.action: "link", CellDataKey.title: item[0], CellDataKey.link: item[1]]
) )
} }
navigationItemTitle = "Special Thanks" navigationItemTitle = "Special Thanks"