ManagingContacts/ManagingContacts/BaseViewController.swift
/* |
Copyright (C) 2017 Apple Inc. All Rights Reserved. |
See LICENSE.txt for this sample’s licensing information |
Abstract: |
A base table view controller to share a data model and a table view |
cell prototype between subclasses. Allows its subclasses to display |
container, contact, and group information. |
*/ |
import UIKit |
class BaseViewController: UITableViewController { |
// MARK: - Types |
fileprivate struct MainStoryboard { |
struct TableViewCellIdentifiers { |
// Cell Identifier used by all subclasses. |
static let cellIdentifier = "cellID" |
} |
} |
// MARK: - Properties |
/// Data model used by all BaseViewController subclasses. |
var data: [MGCModel] = [MGCModel]() { |
didSet { |
tableView.reloadData() |
} |
} |
// MARK: - View Life Cycle |
override func viewDidLoad() { |
super.viewDidLoad() |
} |
// MARK: - UITableViewDataSource |
override func numberOfSections(in tableView: UITableView) -> Int { |
// Return the number of sections. |
return data.count |
} |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
// Return the number of rows in the section. |
return data[section].content.count |
} |
// MARK: - UITableViewDelegate |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
return tableView.dequeueReusableCell(withIdentifier: MainStoryboard.TableViewCellIdentifiers.cellIdentifier, for: indexPath) |
} |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
if let segue = data[indexPath.section].segue, let relatedSegue = segue.related { |
performSegue(withIdentifier: relatedSegue, sender: self) |
} |
} |
// MARK: - Memory Management |
override func didReceiveMemoryWarning() { |
super.didReceiveMemoryWarning() |
} |
} |
Copyright © 2017 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2017-02-11