From aae30271bf384b4b12cee1cc5f24f405f3543cc9 Mon Sep 17 00:00:00 2001 From: Javier Heisekce Date: Mon, 16 Mar 2020 12:14:07 -0300 Subject: [PATCH] Expande seccion --- ContactsApp.xcodeproj/project.pbxproj | 8 ++++---- ContactsApp/ContactStruct.swift | 15 +++++++++++++++ ContactsApp/ContactsViewController.swift | 43 +++++++++++++++++++++++++++++++------------ ContactsApp/File.swift | 13 ------------- 4 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 ContactsApp/ContactStruct.swift delete mode 100644 ContactsApp/File.swift diff --git a/ContactsApp.xcodeproj/project.pbxproj b/ContactsApp.xcodeproj/project.pbxproj index eb025b2..51c1c6f 100644 --- a/ContactsApp.xcodeproj/project.pbxproj +++ b/ContactsApp.xcodeproj/project.pbxproj @@ -12,7 +12,7 @@ CE3351F3241A65D400BA57CA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CE3351F1241A65D400BA57CA /* Main.storyboard */; }; CE3351F5241A65D500BA57CA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CE3351F4241A65D500BA57CA /* Assets.xcassets */; }; CE3351F8241A65D500BA57CA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CE3351F6241A65D500BA57CA /* LaunchScreen.storyboard */; }; - CEC259D8241FAC79005237F5 /* File.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC259D7241FAC79005237F5 /* File.swift */; }; + CEC259D8241FAC79005237F5 /* ContactStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC259D7241FAC79005237F5 /* ContactStruct.swift */; }; CEC9A37F241BFBD000F024EE /* ContactsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */; }; CEC9A381241C096D00F024EE /* SingleContactViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC9A380241C096D00F024EE /* SingleContactViewController.swift */; }; /* End PBXBuildFile section */ @@ -25,7 +25,7 @@ CE3351F4241A65D500BA57CA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; CE3351F7241A65D500BA57CA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; CE3351F9241A65D500BA57CA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - CEC259D7241FAC79005237F5 /* File.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = File.swift; sourceTree = ""; }; + CEC259D7241FAC79005237F5 /* ContactStruct.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactStruct.swift; sourceTree = ""; }; CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsViewController.swift; sourceTree = ""; }; CEC9A380241C096D00F024EE /* SingleContactViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleContactViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -68,7 +68,7 @@ CE3351F9241A65D500BA57CA /* Info.plist */, CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */, CEC9A380241C096D00F024EE /* SingleContactViewController.swift */, - CEC259D7241FAC79005237F5 /* File.swift */, + CEC259D7241FAC79005237F5 /* ContactStruct.swift */, ); path = ContactsApp; sourceTree = ""; @@ -144,7 +144,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - CEC259D8241FAC79005237F5 /* File.swift in Sources */, + CEC259D8241FAC79005237F5 /* ContactStruct.swift in Sources */, CE3351EC241A65D400BA57CA /* AppDelegate.swift in Sources */, CEC9A37F241BFBD000F024EE /* ContactsViewController.swift in Sources */, CE3351EE241A65D400BA57CA /* SceneDelegate.swift in Sources */, diff --git a/ContactsApp/ContactStruct.swift b/ContactsApp/ContactStruct.swift new file mode 100644 index 0000000..acfb810 --- /dev/null +++ b/ContactsApp/ContactStruct.swift @@ -0,0 +1,15 @@ +// +// ContactStruct.swift +// ContactsApp +// +// Created by User on 3/16/20. +// Copyright © 2020 jheisecke. All rights reserved. +// + +import Foundation +import ContactsUI + +struct ContactStruct { + var isExpanded: Bool + let contacts : [CNContact] +} diff --git a/ContactsApp/ContactsViewController.swift b/ContactsApp/ContactsViewController.swift index bb21000..40384ed 100644 --- a/ContactsApp/ContactsViewController.swift +++ b/ContactsApp/ContactsViewController.swift @@ -12,9 +12,9 @@ import ContactsUI class ContactsViewController: UIViewController { @IBOutlet weak var contactsTable: UITableView! - + var contactStruct = [ContactStruct]() var contacts = [CNContact]() - var contactsBackup = [CNContact]() + //var contactsBackup = [CNContact]() let contactStore = CNContactStore() override func viewDidLoad() { @@ -34,6 +34,8 @@ class ContactsViewController: UIViewController { // Array containing all unified contacts from everywhere self.contacts.append(contact) } + let contactsExpandables = ContactStruct(isExpanded: true, contacts: contacts) + self.contactStruct = [contactsExpandables] } catch { print("unable to fetch contacts") @@ -52,12 +54,19 @@ class ContactsViewController: UIViewController { } extension ContactsViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return contacts.count + if !contactStruct[section].isExpanded { + return 0 + } + + return contactStruct[section].contacts.count } - func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "contactName")! - cell.textLabel?.text = "\(contacts[indexPath.row].givenName) \(contacts[indexPath.row].familyName)" + let contactCell = contactStruct[indexPath.section].contacts[indexPath.row] + + cell.textLabel?.text = "\(contactCell.givenName) \(contactCell.familyName)" + return cell } @@ -77,20 +86,30 @@ extension ContactsViewController: UITableViewDelegate, UITableViewDataSource { button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15) button.backgroundColor = .systemGray4 button.addTarget(self, action: #selector(closeSection), for: .touchUpInside) - + button.tag = section return button//ʌΛ } - @objc func closeSection() { - contactsBackup = contacts - let section = 0 + @objc func closeSection(button : UIButton) { + //contactsBackup = contacts + let section = button.tag var indexPaths = [IndexPath]() - for row in contacts.indices { + for row in contactStruct[section].contacts.indices { let indexPath = IndexPath(row: row, section: section) indexPaths.append(indexPath) } - contacts.removeAll() - contactsTable.reloadData() + + let isExpanded = contactStruct[section].isExpanded + contactStruct[section].isExpanded = !isExpanded + + button.setTitle(isExpanded ? "V" : "Λ", for: .normal) + + if isExpanded { + contactsTable.deleteRows(at: indexPaths, with: .top) + } else { + contactsTable.insertRows(at: indexPaths, with: .bottom) + } + } } diff --git a/ContactsApp/File.swift b/ContactsApp/File.swift deleted file mode 100644 index 31d1485..0000000 --- a/ContactsApp/File.swift +++ /dev/null @@ -1,13 +0,0 @@ -// -// File.swift -// ContactsApp -// -// Created by User on 3/16/20. -// Copyright © 2020 jheisecke. All rights reserved. -// - -import Foundation - -class Contacts { - -} -- libgit2 0.26.0