Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ContactManager/ContactManager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
CB8D4C132ACE41B6002132D4 /* ContactBook.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB8D4C122ACE41B6002132D4 /* ContactBook.swift */; };
CB8D4C152ACE4236002132D4 /* Person.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB8D4C142ACE4236002132D4 /* Person.swift */; };
CB8D4C172ACE4D69002132D4 /* WholeListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB8D4C162ACE4D69002132D4 /* WholeListTableViewController.swift */; };
CBA68B3E2ADE2A840064A156 /* ContactInformationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBA68B3D2ADE2A840064A156 /* ContactInformationTableViewCell.swift */; };
CBA735212AD6266100E5E184 /* AddNewContactViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBA735202AD6266100E5E184 /* AddNewContactViewController.swift */; };
CBD4CF8A2AD7C9A7002EA0ED /* InputError.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBD4CF892AD7C9A7002EA0ED /* InputError.swift */; };
/* End PBXBuildFile section */
Expand All @@ -30,6 +31,7 @@
CB8D4C122ACE41B6002132D4 /* ContactBook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactBook.swift; sourceTree = "<group>"; };
CB8D4C142ACE4236002132D4 /* Person.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Person.swift; sourceTree = "<group>"; };
CB8D4C162ACE4D69002132D4 /* WholeListTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WholeListTableViewController.swift; sourceTree = "<group>"; };
CBA68B3D2ADE2A840064A156 /* ContactInformationTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactInformationTableViewCell.swift; sourceTree = "<group>"; };
CBA735202AD6266100E5E184 /* AddNewContactViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddNewContactViewController.swift; sourceTree = "<group>"; };
CBD4CF892AD7C9A7002EA0ED /* InputError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputError.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -68,6 +70,7 @@
children = (
CB8D4C162ACE4D69002132D4 /* WholeListTableViewController.swift */,
CBA735202AD6266100E5E184 /* AddNewContactViewController.swift */,
CBA68B3D2ADE2A840064A156 /* ContactInformationTableViewCell.swift */,
);
path = Controller;
sourceTree = "<group>";
Expand Down Expand Up @@ -184,6 +187,7 @@
files = (
CBA735212AD6266100E5E184 /* AddNewContactViewController.swift in Sources */,
CBD4CF8A2AD7C9A7002EA0ED /* InputError.swift in Sources */,
CBA68B3E2ADE2A840064A156 /* ContactInformationTableViewCell.swift in Sources */,
CB8D4C152ACE4236002132D4 /* Person.swift in Sources */,
CB8D4C172ACE4D69002132D4 /* WholeListTableViewController.swift in Sources */,
CB8D4BFE2ACE3D52002132D4 /* AppDelegate.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// CustomTableViewCell.swift
// ContactManager
//
// Created by 전성수 on 10/17/23.
//

import UIKit

class ContactInformationTableViewCell: UITableViewCell {

@IBOutlet weak var name: UILabel!
@IBOutlet weak var age: UILabel!
@IBOutlet weak var digits: UILabel!

func drawCell(_ person: Person) {
self.name.text = person.name
self.age.text = person.age
self.digits.text = person.digits
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,24 @@
import UIKit

final class WholeListTableViewController: UITableViewController {

private var contactBook = ContactBook()
private var searchFilterdList: [Person] = []
Comment on lines +11 to +13
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 searchFilteredList를 놔두는 것도 괜찮지만, 추후에 코드들이 많아지면 복잡해질 수 있습니다.
기존 연락처와 같이 데이터들을 관리할 객체를 생성해서 처리하는건 어떨까요?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 부분은 오늘 시간이 부족해서 완료하지 못했습니다. 새로 알게된 문제가 하나 있었는데, 검색을 통해서 나온 리스트를 삭제했을 때, 문제가 발생하더라구요.. 그래서 말씀해주신대로 따로 객체를 생성해서 처리하면 이부분도 해결될 것 같다 라고 생각했는데, 일단.. 오늘은 시간이 부족해서 이후에 다시 만들어서 해보도록 하겠습니다!

private let contactCellIdentifier: String = "ContactInformationCell"

private let searchController = UISearchController(searchResultsController: nil)

private var isSearching: Bool {
let isActive = searchController.isActive
let isSearchBarHasText = searchController.searchBar.text?.isEmpty == false
return isActive && isSearchBarHasText
}

override func viewDidLoad() {
super.viewDidLoad()
setUpSearchController()
setPersonContactList()
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contactBook.rowCountContactList()
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contactList", for: indexPath)
let personContact = contactBook.bringPersonContact(indexPath)

cell.textLabel?.text = personContact.name + "(\(personContact.age))"
cell.detailTextLabel?.text = personContact.digits

return cell
}

@IBAction private func moveToAddNewContact(_ sender: Any) {
guard let addNewContactVC = self.storyboard?.instantiateViewController(withIdentifier: "AddNewContactViewController") as? AddNewContactViewController else { return }
Expand All @@ -51,12 +44,77 @@ final class WholeListTableViewController: UITableViewController {
Person(name: "JaeHyuk", age: "88", digits: "010-1234-1234")
])
}

private func setUpSearchController() {
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.placeholder = "이름을 입력해주세요"
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.searchBarStyle = UISearchBar.Style.prominent
searchController.searchBar.sizeToFit()
}
}

extension WholeListTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return searchFilterdList.count
} else {
return contactBook.rowCountContactList()
}
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: contactCellIdentifier, for: indexPath) as? ContactInformationTableViewCell
else { return ContactInformationTableViewCell() }
let personContact = contactBook.bringPersonContact(indexPath)

if isSearching {
let personContactSearched = searchFilterdList[indexPath.row]
cell.drawCell(personContactSearched)
} else {
cell.drawCell(personContact)
}

return cell
}

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
contactBook.deletePersonContact(indexPath)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
}
extension WholeListTableViewController: PersonContactUpdating {
func updateNewPersonContact(name: String, age: String, digits: String) {
let newPersonContact = Person(name: name, age: age, digits: digits)
contactBook.addPersonContact(newPersonContact)
self.tableView.reloadData()
}
}


extension WholeListTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let text = searchController.searchBar.text else {
return
}

searchFilterdList = self.contactBook.personContactList.filter {
$0.name.localizedCaseInsensitiveContains(text)
}

tableView.reloadData()
}
}
Loading