โก ๋ด๊ธฐ ๋ฐ X ๋ฒํผ ๊ตฌํ
1. ๋ด๊ธฐ ๋ฐ X ๋ฒํผ์ ํ๋กํ ๋ฒํผ
X ์ ๋ด๊ธฐ ๋ฒํผ์ ๋๋น ๋น์จ์ 1:3~4
cancelButton.widthAnchor.constraint(equalTo: buttonStackView.widthAnchor, multiplier: 1/4),
cancelButton.heightAnchor.constraint(equalTo: buttonStackView.heightAnchor),
cartButton.widthAnchor.constraint(equalTo: buttonStackView.widthAnchor, multiplier: 3/4),
cartButton.heightAnchor.constraint(equalTo: buttonStackView.heightAnchor)
๋ด๊ธฐ ๋ฒํผ์ ํญ
ํด๋น ์ฑ ์ ๋ด์ ์ฑ ๋ชฉ๋ก ํ๋ฉด์์ ๋ณผ ์ ์์ต๋๋ค.
๋ชจ๋ฌ์ ๋ซํ
[ CoreData CRUD ]
1. ์ฑ ์์ธ ํ๋ฉด์์ ๋ฐ์ดํฐ ์ ์ฅ (Create):
๋ด๊ธฐ ๋ฒํผ ํด๋ฆญ > CoreData์ ์๋ก์ด ์ฑ ์ํฐํฐ๋ฅผ ์์ฑ, ์ ์ฅ
2. ์ฑ ๋ชฉ๋ก ํ๋ฉด์์ ๋ฐ์ดํฐ ์กฐํ (Read):
์ฑ ์์ ์, CoreData์์ ์ ์ฅ๋ ์ฑ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์์ ๋ชฉ๋ก์ ํ์
3. ์ฑ ๋ชฉ๋ก์์ ๋ฐ์ดํฐ ์ญ์ (Delete):
์ญ์ ๋ฒํผ์ ๋๋ฌ CoreData์์ ํด๋น ์ฑ ์ํฐํฐ๋ฅผ ์ญ์
์ฑ ์ ๋ณด๋ ๊ณ ๊ฐ์ ๋ณด ์ ๋ฐ์ดํธ๊ฐ ์๊ธฐ์
U๋ ์ด ํ์ ์์ ํ์๊ฐ ์๋ค!
3. X ๋ฒํผ์ ํญ ํ๋ฉด ๋ชจ๋ฌ์ ๋ซํ
1. Core Data ์ปจํ ์คํธ ๊ฐ์ ธ์ค๊ธฐ:
Core Data ์ปจํ ์คํธ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ์ค์ ํฉ๋๋ค.
2. ๋ด๊ธฐ ๋ฒํผ ํด๋ฆญ ์ก์ ์ถ๊ฐ:
๋ด๊ธฐ ๋ฒํผ์ ํด๋ฆญํ ๋ ISBN์ Core Data์ ์ ์ฅํ๋๋ก ๊ตฌํ
๋ฒํผ ํด๋ฆญ์ ์๋ฟ์ฐฝ ๋์ฐ๊ณ Core Data์ ์ฑ ๋ฒํธ ์ ์ฅ ์๋ฃ
@objc private func cartButtonTapped() {
guard let book = book else {
print("Book is nil")
return
}
if let isbn = book.isbn {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let bookNum = BookNum(context: context)
bookNum.isbn = isbn
do {
try context.save()
print("Book saved to Core Data")
let alert = UIAlertController(title: "์ฑ๊ณต", message: "์ฑ
์ด ๋ด๊ธฐ ๋ชฉ๋ก์ ์ถ๊ฐ๋์์ต๋๋ค.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "ํ์ธ", style: .default))
self.present(alert, animated: true)
} catch {
print("Failed to save book: \(error.localizedDescription)")
}
} else {
print("ISBN is nil")
}
}
โข ์ ํ ๋ฐ ์ถ๊ฐ ๊ตฌํ์ฌํญ
1. ์ฑ ์์ธ ํ๋ฉด์ ์ปจํ ์ธ ์์ ๋ฐ๋ผ ์คํฌ๋กค ํ ์ ์๋๋ก ๊ตฌํ
์ฆ, ์คํฌ๋กค๊ณผ ์๊ด์์ด ํญ์ ํ๋ฉด ์์ ๋ ธ์ถ๋์ด์ผํฉ๋๋ค.
2. ๋ด๊ธฐ ๋ฒํผ ํ ๋ชจ๋ฌ ์ด ๋ซํ ํ ๋ด์ ์ฑ ๋ชฉ๋ก์ ์ถ๊ฐ๋์๋ค๋ alert์ฐฝ ๋์ฐ๊ธฐ
Delegate ํจํด์ ํ์ฉํด๋ด ๋๋ค.
๐ 3 - 2. ๋ด๊ธฐ ํ๋ฉด
โ ๋ด๊ธฐ ํ๋ฉด UI ๊ตฌํ
์ฑ์ ์ข ๋ฃํ๊ณ ๋ค์ ์์ํด๋ ๋ด์ ์ฑ ๋ชฉ๋ก์ ๋จ์์์ด์ผํฉ๋๋ค.
์ ์ฒด ์ญ์ ๋ฒํผ์ ๋๋ฅด๋ฉด ๋ด์๋ ๋ชจ๋ ์ฑ ์ด ์ง์์ง๋๋ค.
์ค์์ดํ ๋ฑ์ ๋ฐฉ์์ ํตํ์ฌ ๋ด์ ์ฑ ๊ฐ๋ณ์ญ์
private func deleteBook(at indexPath: IndexPath) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest: NSFetchRequest<BookNum> = BookNum.fetchRequest()
do {
let bookEntities = try managedContext.fetch(fetchRequest)
let bookToDelete = bookEntities[indexPath.item]
managedContext.delete(bookToDelete)
try managedContext.save()
books.remove(at: indexPath.item)
collectionView.deleteItems(at: [indexPath])
} catch let error as NSError {
handleError(error)
}
}
func didSwipeCell(_ cell: CartListCell) {
if let indexPath = collectionView.indexPath(for: cell) {
showDeleteConfirmation(for: indexPath)
}
}
private func showDeleteConfirmation(for indexPath: IndexPath) {
let alertController = UIAlertController(title: "์ฑ
์ญ์ ", message: "์ด ์ฑ
์ ์ญ์ ํ์๊ฒ ์ต๋๊น?", preferredStyle: .alert)
let deleteAction = UIAlertAction(title: "์ญ์ ", style: .destructive) { [weak self] _ in
self?.deleteBook(at: indexPath)
}
let cancelAction = UIAlertAction(title: "์ทจ์", style: .cancel, handler: nil)
alertController.addAction(deleteAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
private func addSwipeGestureRecognizer() {
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
swipeGesture.direction = .left // Set swipe direction (e.g., left swipe)
contentView.addGestureRecognizer(swipeGesture)
}
@objc private func handleSwipe() {
delegate?.didSwipeCell(self)
}
์ถ๊ฐ๋ก ๊ตฌํํด์ผ ํ๋ ๋ถ๋ถ
โท ๋ด๊ธฐ ๋ฒํผ์ ํญํ๋ฉด alert์ฐฝ์ด ๋จ๊ฒ ๊ตฌํ์ด ๋์ด ์์ alert๋ฒํผ ํด๋ฆญ์ ์์ธ์ ๋ณด ๋ชจ๋ฌ์ ๋ซํ๋๋ก ๊ตฌํ ํ์
โท X ๋ฒํผ์ ํญ ํ๋ฉด ๋ชจ๋ฌ์ ๋ซํ๋๋ก ๊ตฌํ ํ์
โท ์ถ๊ฐ ๋ฒํผ์ ํตํด ๊ฒ์ ํ๋ฉด์ผ๋ก ์ ํํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํด์ผ ํฉ๋๋ค.
โท Level 5 - ๋ฌดํ ์คํฌ๋กค ๊ธฐ๋ฅ ๊ตฌํ, MVVM ๋ฆฌํฉํ ๋ง (์ ํ)
โท ๊ฒ์๋ ๊ฒฐ๊ณผ๋ฅผ ์ต์ ์, ์ ํ๋ ์์ผ๋ก ์ ๋ ฌ
โท 1ํ์ด์ง๋น 20๊ฐ์ ๊ฒฐ๊ณผ๋ฌผ ๋์ถ
โท ๊ฒ์ ํ๋ ์ ํ ํ์ดํ, ์ ์
'iOS_Developer > Team Project' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TeamProject] ํ๋ก์ ํธ ๊ธฐํ _ 1์ผ์ฐจ (0) | 2024.08.23 |
---|---|
[ Project ] ๋จ์ด์ฅ ์ดํ ์ค๋ฅ ํด๊ฒฐ (0) | 2024.08.13 |
[ Project ] ๋จ์ด์ฅ ์ดํ ๊ธฐํ (0) | 2024.08.13 |
[ Project ] Book search APP_2 (0) | 2024.08.08 |
[ Project ] Book search APP (0) | 2024.08.01 |