From 7b0c44ca40b0a839088bdc0fadca9c99df354021 Mon Sep 17 00:00:00 2001 From: max-simon Date: Thu, 5 May 2016 19:59:30 +0200 Subject: [PATCH 1/2] Completed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sekundenzähler neben dem Namen und Navigation. --- Lifetime.xcodeproj/project.pbxproj | 15 +++++- Lifetime/ContactCell.swift | 37 +++++++++++++ Lifetime/ContactDetailViewController.swift | 2 +- Lifetime/ContactListViewController.swift | 46 ++++++++++++++++- Lifetime/LaunchScreen.storyboard | 6 +-- Lifetime/Lifetime.swift | 7 +++ Lifetime/Main.storyboard | 60 +++++++++++++++++++--- 7 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 Lifetime/ContactCell.swift diff --git a/Lifetime.xcodeproj/project.pbxproj b/Lifetime.xcodeproj/project.pbxproj index 31853da..23ac415 100644 --- a/Lifetime.xcodeproj/project.pbxproj +++ b/Lifetime.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 2AA400E31CDB825E00BF7683 /* ContactCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA400E21CDB825E00BF7683 /* ContactCell.swift */; }; 870312411CD4EDBC00A48B90 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8703123F1CD4EDBC00A48B90 /* LaunchScreen.storyboard */; }; 870312421CD4EDBC00A48B90 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 870312401CD4EDBC00A48B90 /* Main.storyboard */; }; 871045011CD4ED800091710A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 871044F21CD4ED800091710A /* AppDelegate.swift */; }; @@ -17,6 +18,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 2AA400E21CDB825E00BF7683 /* ContactCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContactCell.swift; path = Lifetime/ContactCell.swift; sourceTree = SOURCE_ROOT; }; 8703123F1CD4EDBC00A48B90 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = Lifetime/LaunchScreen.storyboard; sourceTree = SOURCE_ROOT; }; 870312401CD4EDBC00A48B90 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Main.storyboard; path = Lifetime/Main.storyboard; sourceTree = SOURCE_ROOT; }; 871044D91CD3C9860091710A /* Lifetime.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Lifetime.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -69,6 +71,7 @@ 870312481CD4EECD00A48B90 /* View */ = { isa = PBXGroup; children = ( + 2AA400E21CDB825E00BF7683 /* ContactCell.swift */, ); name = View; sourceTree = ""; @@ -135,6 +138,7 @@ TargetAttributes = { 871044D81CD3C9860091710A = { CreatedOnToolsVersion = 7.3; + DevelopmentTeam = AVQEU8EC7K; }; }; }; @@ -174,6 +178,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 2AA400E31CDB825E00BF7683 /* ContactCell.swift in Sources */, 8710450A1CD4ED800091710A /* Lifetime.swift in Sources */, 871045071CD4ED800091710A /* ContactDetailViewController.swift in Sources */, 871045011CD4ED800091710A /* AppDelegate.swift in Sources */, @@ -274,10 +279,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = "$(SRCROOT)/Lifetime/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.Life"; + PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.max-simon.life"; PRODUCT_NAME = Lifetime; + PROVISIONING_PROFILE = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; }; name = Debug; @@ -287,10 +295,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; INFOPLIST_FILE = "$(SRCROOT)/Lifetime/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.Life"; + PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.max-simon.life"; PRODUCT_NAME = Lifetime; + PROVISIONING_PROFILE = ""; }; name = Release; }; diff --git a/Lifetime/ContactCell.swift b/Lifetime/ContactCell.swift new file mode 100644 index 0000000..6c13aa7 --- /dev/null +++ b/Lifetime/ContactCell.swift @@ -0,0 +1,37 @@ +// +// ContactCell.swift +// Lifetime +// +// Created by Max Simon on 05.05.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import UIKit +import Contacts + +class ContactCell: UITableViewCell { + + + @IBOutlet weak var labelName: UILabel! + @IBOutlet weak var labelSeconds: UILabel! + + var totalTime: Int = 0 + + func updateSeconds() { + self.totalTime += 1 + labelSeconds.text = String(self.totalTime) + "s" + } + + func configureForContact(contact: CNContact) -> NSTimer? { + labelName.text = contact.givenName + " " + contact.familyName + if let timeSinceBirthday = contact.lifetimeInSeconds { + totalTime = timeSinceBirthday + let cellTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ContactCell.updateSeconds), userInfo: nil, repeats: true) + return cellTimer + } + else { + labelSeconds.text = "" + return nil + } + } +} diff --git a/Lifetime/ContactDetailViewController.swift b/Lifetime/ContactDetailViewController.swift index 8035e55..33aa5fc 100644 --- a/Lifetime/ContactDetailViewController.swift +++ b/Lifetime/ContactDetailViewController.swift @@ -71,7 +71,7 @@ class ContactDetailViewController: UIViewController { lifetimeFormatter.unitsStyle = .SpellOut lifetimeLabel.text = lifetimeFormatter.stringFromDate(birthday, toDate: NSDate()) } else { - lifetimeLabel.text = nil + lifetimeLabel.text = "Sorry, this Birthday is not set 😞" } } diff --git a/Lifetime/ContactListViewController.swift b/Lifetime/ContactListViewController.swift index 7056454..13c5c89 100644 --- a/Lifetime/ContactListViewController.swift +++ b/Lifetime/ContactListViewController.swift @@ -25,6 +25,7 @@ class ContactListViewController: UITableViewController { private var contacts: [CNContact] = [] { didSet { tableView.reloadData() + print(contacts.count) } } @@ -60,6 +61,7 @@ class ContactListViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() + //self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "ContactCell") tableView.tableHeaderView = searchController.searchBar } @@ -68,20 +70,62 @@ class ContactListViewController: UITableViewController { override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { switch segue.identifier! { + case "showInText": + guard let indexPath = (self.view as! UITableView).indexPathForSelectedRow else { return } + segue.destinationViewController.title = contacts[indexPath.row].givenName + " " + contacts[indexPath.row].familyName + (segue.destinationViewController as! ContactDetailViewController).contact = contacts[indexPath.row] + break - // TODO: prepare segue.destinationViewController for each identifier default: break } } + var cellTimers: [NSTimer] = [] + //override func viewDidDisappear(animated: Bool) { + // super.viewDidDisappear(animated) + // for timer in cellTimers { + // timer.invalidate() + // } + // cellTimers.removeAll() + //} + + @IBAction func unwindToRoot(segue: UIStoryboardSegue) { + // nothing to do + } + } // MARK: - Table View Datasource // TODO: implement UITableViewDatasource protocol +extension ContactListViewController { + override func numberOfSectionsInTableView(tableView: UITableView) -> Int { + return 1 + } + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return contacts.count + } + + + override func tableView(tableView: (UITableView!), cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + let contact = contacts[indexPath.row] + let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell", forIndexPath: indexPath) as! ContactCell + if let cellTimer = cell.configureForContact(contact) { + self.cellTimers.append(cellTimer) + cell.selectionStyle = .Default + cell.accessoryType = .DisclosureIndicator + } else { + cell.selectionStyle = .None + cell.accessoryType = .None + } + + return cell + + } +} // MARK: - Search Results Updating diff --git a/Lifetime/LaunchScreen.storyboard b/Lifetime/LaunchScreen.storyboard index 2e721e1..323bd43 100644 --- a/Lifetime/LaunchScreen.storyboard +++ b/Lifetime/LaunchScreen.storyboard @@ -1,7 +1,8 @@ - + - + + @@ -15,7 +16,6 @@ - diff --git a/Lifetime/Lifetime.swift b/Lifetime/Lifetime.swift index 75a7858..8c9b7f7 100644 --- a/Lifetime/Lifetime.swift +++ b/Lifetime/Lifetime.swift @@ -22,4 +22,11 @@ extension CNContact { return NSDate().timeIntervalSinceDate(birthday) } + var lifetimeInSeconds: Int? { + guard let birthdate = self.birthday, birthdayAsNSObject = NSCalendar.currentCalendar().dateFromComponents(birthdate) else { + return nil + } + let totalTime = NSDate().timeIntervalSinceDate(birthdayAsNSObject) + return Int(totalTime) + } } diff --git a/Lifetime/Main.storyboard b/Lifetime/Main.storyboard index f56967f..7412cde 100644 --- a/Lifetime/Main.storyboard +++ b/Lifetime/Main.storyboard @@ -21,7 +21,7 @@ - + @@ -33,16 +33,16 @@ - - + + - + + + + + + + + + + + + @@ -60,13 +72,14 @@ - + + @@ -78,6 +91,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + From c473ca00d1146e545a947982068a3496ec703bff Mon Sep 17 00:00:00 2001 From: max-simon Date: Thu, 5 May 2016 21:59:58 +0200 Subject: [PATCH 2/2] Fix Recyclebug and Textoverflow --- Lifetime/ContactCell.swift | 9 +++++---- Lifetime/ContactListViewController.swift | 21 ++++++--------------- Lifetime/Main.storyboard | 6 +++++- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Lifetime/ContactCell.swift b/Lifetime/ContactCell.swift index 6c13aa7..a7797cb 100644 --- a/Lifetime/ContactCell.swift +++ b/Lifetime/ContactCell.swift @@ -16,22 +16,23 @@ class ContactCell: UITableViewCell { @IBOutlet weak var labelSeconds: UILabel! var totalTime: Int = 0 + var timer: NSTimer? func updateSeconds() { self.totalTime += 1 labelSeconds.text = String(self.totalTime) + "s" } - func configureForContact(contact: CNContact) -> NSTimer? { + func configureForContact(contact: CNContact) { labelName.text = contact.givenName + " " + contact.familyName if let timeSinceBirthday = contact.lifetimeInSeconds { totalTime = timeSinceBirthday - let cellTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ContactCell.updateSeconds), userInfo: nil, repeats: true) - return cellTimer + timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ContactCell.updateSeconds), userInfo: nil, repeats: true) } else { labelSeconds.text = "" - return nil + totalTime = 0 + timer = nil } } } diff --git a/Lifetime/ContactListViewController.swift b/Lifetime/ContactListViewController.swift index 13c5c89..2e8efc6 100644 --- a/Lifetime/ContactListViewController.swift +++ b/Lifetime/ContactListViewController.swift @@ -82,14 +82,6 @@ class ContactListViewController: UITableViewController { } } - var cellTimers: [NSTimer] = [] - //override func viewDidDisappear(animated: Bool) { - // super.viewDidDisappear(animated) - // for timer in cellTimers { - // timer.invalidate() - // } - // cellTimers.removeAll() - //} @IBAction func unwindToRoot(segue: UIStoryboardSegue) { // nothing to do @@ -113,14 +105,13 @@ extension ContactListViewController { override func tableView(tableView: (UITableView!), cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let contact = contacts[indexPath.row] let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell", forIndexPath: indexPath) as! ContactCell - if let cellTimer = cell.configureForContact(contact) { - self.cellTimers.append(cellTimer) - cell.selectionStyle = .Default - cell.accessoryType = .DisclosureIndicator - } else { - cell.selectionStyle = .None - cell.accessoryType = .None + // Weiterlaufen verhindern + if let timerOldCell = cell.timer { + timerOldCell.invalidate() } + cell.configureForContact(contact) + cell.selectionStyle = .Default + cell.accessoryType = .DisclosureIndicator return cell diff --git a/Lifetime/Main.storyboard b/Lifetime/Main.storyboard index 7412cde..a689734 100644 --- a/Lifetime/Main.storyboard +++ b/Lifetime/Main.storyboard @@ -107,14 +107,18 @@ - + +