@objc(PokemonPhoneBook)
public class PokemonPhoneBook: NSManagedObject {
public static let className = "PokemonPhoneBook"
public enum Key {
static let name = "name"
static let phoneNumber = "phoneNumber"
static let profileImage = "profileImage"
}
}
class ContactCell: UITableViewCell {
private let profileImageView = UIImageView()
private let nameLabel = UILabel()
private let phoneLabel = UILabel()
private func setupCell() {
profileImageView.layer.cornerRadius = 25
profileImageView.clipsToBounds = true
// SnapKit을 활용한 Auto Layout
profileImageView.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.leading.equalToSuperview().offset(40)
make.width.height.equalTo(50)
}
// ... 나머지 제약조건
}
}
func configure(with contact: PokemonPhoneBook) {
if let imageString = contact.profileImage,
let imageData = Data(base64Encoded: imageString),
let image = UIImage(data: imageData) {
profileImageView.image = image
} else {
profileImageView.image = nil
profileImageView.backgroundColor = .lightGray
}
}
@objc private func randomPokemonButtonTapped() {
let randomId = Int.random(in: 1...1000)
let urlString = "https://pokeapi.co/api/v2/pokemon/\(randomId)"
URLSession.shared.dataTask(with: URL(string: urlString)!) { [weak self] data, response, error in
// API 응답 처리
DispatchQueue.main.async {
self?.profileImageView.image = UIImage(data: imageData)
}
}.resume()
}
#Preview {
UINavigationController(rootViewController: ViewController())
}
이미지 선택 관련
전화번호 포맷팅
// 추가 예정인 포맷팅 함수
func formatPhoneNumber(_ number: String) -> String {
// 구현 예정
}
테이블뷰 레이아웃
UI 개선사항