Last active
August 29, 2015 14:12
-
-
Save toddkramer/3775f4ac96ce27c02bf8 to your computer and use it in GitHub Desktop.
ExtensionDataSharing06-TodayViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import NotificationCenter | |
import Books | |
class TodayViewController: UIViewController, NCWidgetProviding, UITableViewDataSource, UITableViewDelegate { | |
@IBOutlet var tableView: UITableView! | |
var booksArray = [Book]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.preferredContentSize = CGSizeMake(320, 180); | |
readPropertyList() | |
} | |
func titleFromSharedDefaults() -> String { | |
let sharedDefaults = NSUserDefaults(suiteName: "group.example.DataSharing")! | |
if let titleText = sharedDefaults.stringForKey("ExtensionTitleKey") { | |
return titleText | |
} | |
return "" | |
} | |
func readPropertyList() { | |
if let filePath = SharedBooksManager.urlForSharedBooksArray() { | |
booksArray = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as [Book] | |
} | |
} | |
func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return 1 | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return booksArray.count | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) | |
-> UITableViewCell { | |
let currentBook = booksArray[indexPath.row] | |
let cell = tableView.dequeueReusableCellWithIdentifier("CSBookCell") as UITableViewCell | |
cell.textLabel?.text = currentBook.title | |
cell.detailTextLabel?.text = currentBook.author | |
return cell | |
} | |
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
return titleFromSharedDefaults() | |
} | |
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
return 40 | |
} | |
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { | |
view.tintColor = UIColor.clearColor() | |
let headerView = (view as UITableViewHeaderFooterView) | |
headerView.textLabel.textColor = UIColor.whiteColor() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment