diff --git a/CheoMooRac/CheoMooRac/Sources/Cells/MyCardTableViewCell.swift b/CheoMooRac/CheoMooRac/Sources/Cells/MyCardTableViewCell.swift new file mode 100644 index 0000000..bae00a7 --- /dev/null +++ b/CheoMooRac/CheoMooRac/Sources/Cells/MyCardTableViewCell.swift @@ -0,0 +1,70 @@ +// +// MyCardTableViewCell.swift +// CheoMooRac +// +// Created by 김윤서 on 2021/09/14. +// + +import UIKit + +class MyCardTableViewCell: UITableViewCell, Reusable { + + private let profileImageView = UIImageView().then { + $0.clipsToBounds = true + $0.backgroundColor = .yellow + } + + private let nameLabel = UILabel().then { + $0.text = "김윤서" + $0.font = .boldSystemFont(ofSize: 15) + } + + private let discriptionLabel = UILabel().then { + $0.text = "내 카드" + $0.font = .systemFont(ofSize: 10) + $0.textColor = .lightGray + } + + private let vStackView = UIStackView().then { + $0.axis = .vertical + $0.alignment = .leading + $0.spacing = 4 + } + + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { + super.init(style: style, reuseIdentifier: reuseIdentifier) + setLayouts() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func setLayouts() { + setViewHierarchy() + setConstraints() + } + + private func setViewHierarchy() { + contentView.addSubviews(profileImageView, vStackView) + vStackView.addArrangedSubviews(nameLabel,discriptionLabel) + } + + private func setConstraints() { + + profileImageView.snp.makeConstraints { + $0.width.height.equalTo(40) + $0.top.equalToSuperview().offset(20) + $0.leading.equalToSuperview().offset(20) + } + + profileImageView.layer.cornerRadius = 20 + + vStackView.snp.makeConstraints { + $0.centerY.equalToSuperview() + $0.leading.equalTo(profileImageView.snp.trailing).inset(-20) + } + + } + +} diff --git a/CheoMooRac/CheoMooRac/Sources/ViewControllers/ViewController.swift b/CheoMooRac/CheoMooRac/Sources/ViewControllers/ViewController.swift index b530cb8..0bcb019 100644 --- a/CheoMooRac/CheoMooRac/Sources/ViewControllers/ViewController.swift +++ b/CheoMooRac/CheoMooRac/Sources/ViewControllers/ViewController.swift @@ -7,13 +7,152 @@ import UIKit + +import SnapKit +import Then + class ViewController: UIViewController { + + private let tableView = UITableView() + + private var filteredArr: [String] = [] + var arr = ["Zedd", "Alan Walker", "David Guetta", "Avicii", "Marshmello", "Steve Aoki", "R3HAB", "Armin van Buuren", "Skrillex", "Illenium", "The Chainsmokers", "Don Diablo", "Afrojack", "Tiesto", "KSHMR", "DJ Snake", "Kygo", "Galantis", "Major Lazer", "Vicetone" ] + + var isFiltering: Bool { + let searchController = self.navigationItem.searchController + let isActive = searchController?.isActive ?? false + let isSearchBarHasText = searchController?.searchBar.text?.isEmpty == false + return isActive && isSearchBarHasText + } override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + initViewController() + + setLayouts() + + setSearchController() + setTableView() + setRefreshControl() + + } + + private func initViewController() { + title = "연락처" + view.backgroundColor = .white } + + private func setRefreshControl() { + let refresh = UIRefreshControl() + refresh.addTarget(self, action: #selector(tableViewDidPulled(refresh:)), for: .valueChanged) + tableView.refreshControl = refresh + } + + private func setSearchController() { + let searchController = UISearchController(searchResultsController: nil) + navigationItem.searchController = searchController + navigationItem.title = "연락처" + navigationItem.hidesSearchBarWhenScrolling = false + navigationController?.navigationBar.prefersLargeTitles = true + navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addButtonDidTapped)) + + searchController.searchResultsUpdater = self + } + + private func setTableView() { + tableView.delegate = self + tableView.dataSource = self + + tableView.registerReusableCell(MyCardTableViewCell.self) + } + + private func setLayouts() { + setViewHierarchy() + setConstraints() + } + + private func setViewHierarchy() { + view.addSubview(tableView) + } + + private func setConstraints() { + tableView.snp.makeConstraints { + $0.edges.equalToSuperview() + } + } } +extension ViewController { + @objc + private func addButtonDidTapped() { + let createViewController = CreateViewController() + let navCreateViewController: UINavigationController = UINavigationController(rootViewController: createViewController) + present(navCreateViewController, animated: true, completion: nil) + } + + @objc + private func tableViewDidPulled(refresh: UIRefreshControl) { + refresh.endRefreshing() + tableView.reloadData() + } +} + +extension ViewController: UITableViewDelegate { + func numberOfSections(in tableView: UITableView) -> Int { + return 2 + } + + func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { + switch indexPath.section { + case 0: + return 100 + case 1: + return 48 + default: + return 0 + } + } +} + +extension ViewController: UITableViewDataSource { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + switch section { + case 0 : + return 1 + case 1 : + return self.isFiltering ? self.filteredArr.count : self.arr.count + default: + return 0 + } + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + switch indexPath.section { + case 0: + let cell = tableView.dequeueReusableCell(indexPath: indexPath) as MyCardTableViewCell + return cell + case 1: + let cell = UITableViewCell() + if self.isFiltering { + cell.textLabel?.text = self.filteredArr[indexPath.row] + } else { + cell.textLabel?.text = self.arr[indexPath.row] + } + return cell + default: + return UITableViewCell() + } + } + +} + +extension ViewController: UISearchResultsUpdating{ + func updateSearchResults(for searchController: UISearchController) { + guard let text = searchController.searchBar.text else { return } + filteredArr = arr.filter { $0.localizedCaseInsensitiveContains(text) } + tableView.reloadData() + dump(filteredArr) + } +}