Tuesday, July 21, 2026
HomeiOS Developmentios - The right way to change the worth for a particular...

ios – The right way to change the worth for a particular cell

[ad_1]

Good night! Are you able to please inform me methods to make it potential to save lots of like/in contrast to values for every cell? I exploit UserDefaults, however thus far I can solely set the worth for all cells without delay. I perceive that I have to configure one way or the other this second in cellForRowAt , however I don’t perceive methods to do it.

My ViewController:

struct Likes {
    let id: Int
    let isLike: Bool
}

closing class ListTicketsViewController: UIViewController {
    
    var likesDict: [Int:Bool] = [:]
    var likesArray: [Likes] = []

...

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ListTicketsModulCell
        let ticket = tickets[indexPath.row]
        cell.configure(with: ticket)
        
        likesDict.updateValue(ButtonSettings.isButtonTapped, forKey: indexPath.row)
        
        for (key, worth) in likesDict {
            likesArray.append(Likes(id: key, isLike: worth))
        }
        
        let cellLike = likesArray[indexPath.row]
        cell.isButtonPressed = cellLike.isLike
        
        cell.backgroundColor = UIColor.systemBlue
        return cell
    }

My cell:

class ListTicketsModulCell: UITableViewCell {
    
    var isButtonPressed: Bool = false
    
    override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(model: model, reuseIdentifier: reuseIdentifier)
        
        isButtonPressed = ButtonSettings.isButtonTapped
        
    }

...

@objc func likeButtonPressed() {
        if isButtonPressed == false {
            likeButton.setImage(UIImage(systemName: "coronary heart.fill"), for: .regular)
            likeButton.tintColor = .crimson
            ButtonSettings.isButtonTapped = true
        } else if isButtonPressed == true {
            likeButton.setImage(UIImage(systemName: "coronary heart"), for: .regular)
            likeButton.tintColor = .white
            ButtonSettings.isButtonTapped = false
        }
    }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments