passforios/pass/Controllers/BasicStaticTableViewController.swift

77 lines
2.7 KiB
Swift
Raw Normal View History

2017-02-09 01:41:17 +08:00
//
// BasicTableViewController.swift
// pass
//
// Created by Mingshen Sun on 9/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
import SafariServices
2017-02-09 01:41:17 +08:00
enum CellDataType {
case link, segue, empty
}
enum CellDataKey {
2017-02-09 11:12:14 +08:00
case type, title, link, footer, accessoryType, detailDisclosureAction, detailDisclosureData
2017-02-09 01:41:17 +08:00
}
class BasicStaticTableViewController: UITableViewController {
var tableData = [[Dictionary<CellDataKey, Any>]]()
var navigationItemTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = navigationItemTitle
}
override func numberOfSections(in tableView: UITableView) -> Int {
return tableData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData[section].count
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let cellData = tableData[indexPath.section][indexPath.row]
cell.textLabel?.text = cellData[CellDataKey.title] as? String
2017-02-09 11:12:14 +08:00
if let accessoryType = cellData[CellDataKey.accessoryType] as? UITableViewCellAccessoryType {
cell.accessoryType = accessoryType
} else {
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
}
2017-02-09 01:41:17 +08:00
return cell
}
2017-02-09 11:12:14 +08:00
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
let cellData = tableData[indexPath.section][indexPath.row]
let selector = cellData[CellDataKey.detailDisclosureAction] as? Selector
perform(selector, with: cellData[CellDataKey.detailDisclosureData])
}
2017-02-09 01:41:17 +08:00
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cellData = tableData[indexPath.section][indexPath.row]
let cellDataType = cellData[CellDataKey.type] as? CellDataType
switch cellDataType! {
case .segue:
let link = cellData[CellDataKey.link] as? String
performSegue(withIdentifier: link!, sender: self)
case .link:
let link = cellData[CellDataKey.link] as! String
let svc = SFSafariViewController(url: URL(string: link)!, entersReaderIfAvailable: true)
self.present(svc, animated: true, completion: nil)
2017-02-09 01:41:17 +08:00
default:
break
}
}
}