카테고리 없음
23.09.15 연락처 만들기 진행상황 공유
oong2
2023. 9. 16. 01:38
개인적으로 더 공부를 하기위해서 만들고있는 아이폰 기본 연락처 앱.
UI와 기능들 모두 구현하는게 최종 목표이다.
무사히 해왔던것을 토대로 tableView 에 tableViewCell 을 잘 추가하였다.
단순히 추가만한것이아닌 이름별로 구현되게끔까진 해놓았는데 이 다음인 + 버튼을 눌러
저 array에 새로운 항목을 추가시키는것이 관건이 될 것 같다.
alert의 작동방식과 , tableView의 개념이 조금씩 더 잘 잡혀가는것같아 뿌듯하다.
우선 tableVIew에 Cell 을 추가하는게 swift 파일을 새로 생성하여 그곳에서 관리하는걸 개념이 조금 잡혔고 ,
data 의 갯수만큼 cell이 생성되어야 하는것도 이젠 알겠다.
아래는 코드 전문 👇 ( configure로 정리 안했음 )
//
// ViewController.swift
// PhoneNumber
//
// Created by t2023-m0088 on 2023/08/23.
//
import UIKit
import SwiftUI
class ViewController: UIViewController, UITableViewDelegate {
// var data = [[String]]()
let data = [
"이명박",
"박근혜",
"이재명",
"윤석열",
"김대중",
"김영삼",
"노태우",
"이명박",
"박근혜",
"이재명",
"윤석열",
"김대중",
"김영삼",
"노태우",
]
let header : UILabel = {
let view = UILabel()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let plusBtn : UIButton = {
let view = UIButton()
view.translatesAutoresizingMaskIntoConstraints = false
view.setTitle("+", for: .normal)
view.setTitleColor(.systemGray, for: .normal)
view.titleLabel?.font = UIFont.systemFont(ofSize: 40)
return view
}()
let search : UISearchBar = {
let view = UISearchBar()
view.backgroundColor = .systemGray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let myCardImage : UIImageView = {
let view = UIImageView()
view.layer.cornerRadius = 40
view.translatesAutoresizingMaskIntoConstraints = false
view.image = UIImage(named: "christ")
view.clipsToBounds = true
return view
}()
let myCard : UILabel = {
let view = UILabel()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let tableView : UITableView = {
let view = UITableView()
view.backgroundColor = .lightGray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(header)
self.view.addSubview(search)
self.view.addSubview(myCardImage)
self.view.addSubview(myCard)
self.view.addSubview(tableView)
self.view.backgroundColor = .white
tableView.delegate = self
tableView.dataSource = self
tableView.register(ContactTableViewCell.self, forCellReuseIdentifier: "Cell")
configurePlusBtn()
header.heightAnchor.constraint(equalToConstant: 80).isActive = true
header.widthAnchor.constraint(equalToConstant: 350).isActive = true
header.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
header.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 80).isActive = true
header.text = "연락처"
header.textColor = .darkGray
header.font = UIFont.systemFont(ofSize: 30, weight: .bold)
search.heightAnchor.constraint(equalToConstant: 60).isActive = true
search.widthAnchor.constraint(equalToConstant: 350).isActive = true
search.topAnchor.constraint(equalTo: header.bottomAnchor, constant: 0).isActive = true
search.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
search.text = "검색"
myCardImage.heightAnchor.constraint(equalToConstant: 80).isActive = true
myCardImage.widthAnchor.constraint(equalToConstant: 80).isActive = true
myCardImage.topAnchor.constraint(equalTo: search.bottomAnchor, constant: 10).isActive = true
myCardImage.leadingAnchor.constraint(equalTo: header.leadingAnchor).isActive = true
myCardImage.contentMode = .scaleAspectFill
myCard.heightAnchor.constraint(equalToConstant: 100).isActive = true
myCard.widthAnchor.constraint(equalToConstant: 200).isActive = true
myCard.text = "내 카드"
myCard.textColor = .darkGray
myCard.leftAnchor.constraint(equalTo: myCardImage.rightAnchor, constant: 15).isActive = true
myCard.topAnchor.constraint(equalTo: search.bottomAnchor, constant: 0).isActive = true
myCard.font = UIFont.systemFont(ofSize: 16)
tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: myCard.bottomAnchor, constant: 20).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
tableView.widthAnchor.constraint(equalToConstant: 350).isActive = true
plusBtn.addTarget(self, action: #selector(plusBtnClick), for: .touchUpInside)
// let sectionList = ["ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ","ㅁ","ㅂ","ㅃ","ㅅ","ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"]
//
// func sectionIndexTitles(for tableView: UITableView) -> [String]? {
// return sectionList
// }
//
func configurePlusBtn(){
self.view.addSubview(plusBtn)
NSLayoutConstraint.activate([
plusBtn.centerYAnchor.constraint(equalTo: header.centerYAnchor, constant: 0),
plusBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
plusBtn.widthAnchor.constraint(equalToConstant: 40),
plusBtn.heightAnchor.constraint(equalToConstant: 40)
])
}
}
@objc func plusBtnClick(){
// let cardViewController = cardViewController()
// self.present(cardViewController, animated: true)
let alert = UIAlertController(title: "연락처 추가", message: "정보를 입력하세요", preferredStyle: .alert)
print("sdas")
let success = UIAlertAction(title: "확인", style: .default)
let cancel = UIAlertAction(title: "취소", style: .cancel, handler: nil)
self.present(alert, animated: true, completion: nil)
alert.addAction(success)
alert.addAction(cancel)
alert.addTextField(configurationHandler: {TextField in TextField.placeholder = "연락처를 입력하세요"})
// data.append(["dsd"])
}
}
extension ViewController:UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ContactTableViewCell
let item = data[indexPath.row]
cell.selectionStyle = .none
cell.textLabel?.text = item
return cell
}
// func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// return "\(section)"
// }
// func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
// return 1
// }
}
#if DEBUG
import SwiftUI
struct ViewControllerRepresentable: UIViewControllerRepresentable{
// update
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
@available(iOS 13.0, *)
func makeUIViewController(context: Context) -> UIViewController {
ViewController()
}
// makeui
}
struct ViewController_Previews: PreviewProvider{
static var previews: some View{
ViewControllerRepresentable()
.previewDisplayName("아이폰 14")
}
}
#endif