Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ’…๐Ÿป 7์ฃผ์ฐจ ์„ธ๋ฏธ๋‚˜ #16

Merged
merged 14 commits into from
Aug 21, 2023

Conversation

ena-isme
Copy link
Contributor

@ena-isme ena-isme commented Aug 20, 2023

7์ฃผ์ฐจ

7์ฃผ์ฐจ ์„ธ๋ฏธ๋‚˜์—์„œ๋Š” Realm ์„ ํ†ตํ•˜์—ฌ LocalDB์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฐ€์ ธ์˜ค๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


์ฃผ์š”์ฝ”๋“œ


Realm ์— ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.


class CarrotResult: Object {

    @Persisted(primaryKey: true) var id: Int
    @Persisted var image: String
    @Persisted var product: String
    @Persisted var place: String
    @Persisted var time: String
    @Persisted var tradeStatus: String
    @Persisted var price: Int
    @Persisted var liked: Bool

    convenience init(id: Int, image: String, product: String, place: String, time: String, tradeStatus: String, price: Int, liked: Bool) {

        self.init()

        self.id = id
        self.image = image
        self.product = product
        self.place = place
        self.time = time
        self.tradeStatus = tradeStatus
        self.price = price
        self.liked = liked
    }
}
//CarrotVIewController ๋‚ด cellForRowAt ํ•จ์ˆ˜

let localRealm = try! Realm()
cell.configureCell(dummy[indexPath.row])
let taskToUpdate = dummy[indexPath.row]
cell.handler = {[weak self] in
       guard let self else { return }
       try! localRealm.write {
             print("\(indexPath.row) ์ง  \(taskToUpdate.liked)")
             if taskToUpdate.liked == true {
                    taskToUpdate.liked = false
                    tableView.reloadData()
             }
            else {
                    taskToUpdate.liked = true
                    tableView.reloadData()
           }
     }
}

์ข‹์•„์š” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์ด๋ฅผ ๋ฐ˜์˜ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


var handler : (() -> (Void))?
var ButtonTap : Bool = false {
        didSet {
            buttonTap()
        }
}

likedButton.do {
            $0.tintColor = .systemGreen
            $0.addTarget(self, action: #selector(likedButtonTapped), for: .touchUpInside)
 }

func buttonTap() {
        if ButtonTap == true {
            likedButton.setImage(UIImage(systemName: "star.fill"), for: .normal)
        }
        else {
            likedButton.setImage(UIImage(systemName: "star"), for: .normal)
        }
}

@objc
    func likedButtonTapped() {
        handler?()
}

์‹ค์Šต๊ฒฐ๊ณผ


Simulator.Screen.Recording.-.iPhone.14.Pro.-.2023-08-21.at.13.20.09.mp4

@ena-isme ena-isme merged commit 4585041 into main Aug 21, 2023
@ena-isme ena-isme self-assigned this Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant