質問編集履歴
1
解決いたしましたので削除します。
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,223 +1,2 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            タイトル通り、Realmでライク機能を実装したいのですが、以下の不具合が解消されずに困っています。
         
     | 
| 
       2 
     | 
    
         
            -
            ・ **画面遷移を行うと、勝手にlikeボタンの色が変わったり、他のセルのlikeボタンの色もタッチしていないのに変わる**
         
     | 
| 
      
 2 
     | 
    
         
            +
            ・ **画面遷移を行うと、勝手にlikeボタンの色が変わったり、他のセルのlikeボタンの色もタッチしていないのに変わる**
         
     | 
| 
       3 
     | 
    
         
            -
             
     | 
| 
       4 
     | 
    
         
            -
            こちらは本あぷりのモデルです。
         
     | 
| 
       5 
     | 
    
         
            -
            ```ここに言語を入力
         
     | 
| 
       6 
     | 
    
         
            -
            class Tweet: Object {
         
     | 
| 
       7 
     | 
    
         
            -
                
         
     | 
| 
       8 
     | 
    
         
            -
                @objc dynamic var tweetId: Int = 0
         
     | 
| 
       9 
     | 
    
         
            -
                @objc dynamic var caption: String = ""
         
     | 
| 
       10 
     | 
    
         
            -
                @objc dynamic var timestamp: Date = Date()
         
     | 
| 
       11 
     | 
    
         
            -
                @objc dynamic var retweetCount: Int = 0
         
     | 
| 
       12 
     | 
    
         
            -
                @objc dynamic var likes: Int = 0
         
     | 
| 
       13 
     | 
    
         
            -
                @objc dynamic var didLike = false
         
     | 
| 
       14 
     | 
    
         
            -
                @objc dynamic var replyingTo: String?
         
     | 
| 
       15 
     | 
    
         
            -
                @objc dynamic var isReply: Bool { return replyingTo != nil }
         
     | 
| 
       16 
     | 
    
         
            -
                
         
     | 
| 
       17 
     | 
    
         
            -
                // Userオブジェクトを取得する
         
     | 
| 
       18 
     | 
    
         
            -
                let users = LinkingObjects(fromType: User.self, property: "tweets")
         
     | 
| 
       19 
     | 
    
         
            -
                
         
     | 
| 
       20 
     | 
    
         
            -
                override static func primaryKey() -> String? {
         
     | 
| 
       21 
     | 
    
         
            -
                    return "tweetId"
         
     | 
| 
       22 
     | 
    
         
            -
                }
         
     | 
| 
       23 
     | 
    
         
            -
                
         
     | 
| 
       24 
     | 
    
         
            -
            }
         
     | 
| 
       25 
     | 
    
         
            -
            ```
         
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
            そしてこのVCの関数 **handleLikeTapped(_ cell: TweetCell)**が問題があります。
         
     | 
| 
       28 
     | 
    
         
            -
            この関数はカスタムセル(TweetCell)のボタン(likeButton)をタップすると、色が変え、ライクした回数を数えます。
         
     | 
| 
       29 
     | 
    
         
            -
            falseだと、回数をマイナスし、trueだとプラスします。
         
     | 
| 
       30 
     | 
    
         
            -
            しかし、ここでおかしな問題が生じます。
         
     | 
| 
       31 
     | 
    
         
            -
            ボタンが二回押さないと赤にならず、またfalseだと.lightgrayになるのですが、falseだとtrueになってしまい、そのまたtrueだとfalseにという矛盾が生じてしまっています。そして、アプリを終了し、また起動してみると、勝手に色が変わり、タッチしていないセルのボタンの色も変わり、trueになっていないにもかかわらずもです。
         
     | 
| 
       32 
     | 
    
         
            -
            もはや意味が分からないので、どなたか対処方法を教えてくださると幸いです。
         
     | 
| 
       33 
     | 
    
         
            -
            ```ここに言語を入力
         
     | 
| 
       34 
     | 
    
         
            -
            import UIKit
         
     | 
| 
       35 
     | 
    
         
            -
            import RealmSwift
         
     | 
| 
       36 
     | 
    
         
            -
            import ActiveLabel
         
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
            private let reuseIdentifier = "TweetCell"
         
     | 
| 
       39 
     | 
    
         
            -
            private let headeerIdentifier = "TweetHeader"
         
     | 
| 
       40 
     | 
    
         
            -
            private let realm = try! Realm()
         
     | 
| 
       41 
     | 
    
         
            -
            private let userObject = Array(realm.objects(User.self))
         
     | 
| 
       42 
     | 
    
         
            -
            private let tweetObject = Array(realm.objects(Tweet.self))
         
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
            class FeedController: UICollectionViewController {
         
     | 
| 
       45 
     | 
    
         
            -
               
         
     | 
| 
       46 
     | 
    
         
            -
                private var profileImage: UIImage?
         
     | 
| 
       47 
     | 
    
         
            -
                var viewControllers: [UIViewController] = []
         
     | 
| 
       48 
     | 
    
         
            -
                public var user: Results<User>! {
         
     | 
| 
       49 
     | 
    
         
            -
                    didSet {
         
     | 
| 
       50 
     | 
    
         
            -
                        guard let nav = viewControllers[0] as? UINavigationController else { return }
         
     | 
| 
       51 
     | 
    
         
            -
                        guard let feed = nav.viewControllers.first as? FeedController else { return }
         
     | 
| 
       52 
     | 
    
         
            -
                        feed.user = user
         
     | 
| 
       53 
     | 
    
         
            -
                        configureLeftBarButton()
         
     | 
| 
       54 
     | 
    
         
            -
                    }
         
     | 
| 
       55 
     | 
    
         
            -
                }
         
     | 
| 
       56 
     | 
    
         
            -
                
         
     | 
| 
       57 
     | 
    
         
            -
                private var tweets: [Tweet] = [Tweet]() {
         
     | 
| 
       58 
     | 
    
         
            -
                    didSet { collectionView.reloadData() }
         
     | 
| 
       59 
     | 
    
         
            -
                }
         
     | 
| 
       60 
     | 
    
         
            -
                
         
     | 
| 
       61 
     | 
    
         
            -
                let actionButton: UIButton = {
         
     | 
| 
       62 
     | 
    
         
            -
                    let button = UIButton(type: .system)
         
     | 
| 
       63 
     | 
    
         
            -
                    button.tintColor = .white
         
     | 
| 
       64 
     | 
    
         
            -
                    button.backgroundColor = .twitterBlue
         
     | 
| 
       65 
     | 
    
         
            -
                    button.setImage(UIImage(named: "baseline_playlist_add_white_36pt_1x"), for: .normal)
         
     | 
| 
       66 
     | 
    
         
            -
                    button.addTarget(self, action: #selector(actionButtonTapped), for: .touchUpInside)
         
     | 
| 
       67 
     | 
    
         
            -
                    return button
         
     | 
| 
       68 
     | 
    
         
            -
                }()
         
     | 
| 
       69 
     | 
    
         
            -
             
     | 
| 
       70 
     | 
    
         
            -
                //MARK: - LifeCycle
         
     | 
| 
       71 
     | 
    
         
            -
                
         
     | 
| 
       72 
     | 
    
         
            -
                override func viewDidLoad() {
         
     | 
| 
       73 
     | 
    
         
            -
                    super.viewDidLoad()
         
     | 
| 
       74 
     | 
    
         
            -
                    view.backgroundColor = .white
         
     | 
| 
       75 
     | 
    
         
            -
                    self.setRealm()
         
     | 
| 
       76 
     | 
    
         
            -
                }
         
     | 
| 
       77 
     | 
    
         
            -
                
         
     | 
| 
       78 
     | 
    
         
            -
                public func setRealm(){
         
     | 
| 
       79 
     | 
    
         
            -
                    tweets = Array(realm.objects(Tweet.self))
         
     | 
| 
       80 
     | 
    
         
            -
                    collectionView.reloadData()
         
     | 
| 
       81 
     | 
    
         
            -
                }
         
     | 
| 
       82 
     | 
    
         
            -
                
         
     | 
| 
       83 
     | 
    
         
            -
            }
         
     | 
| 
       84 
     | 
    
         
            -
             
     | 
| 
       85 
     | 
    
         
            -
            // MARK: - UICollectionViewDelegate/DataSource
         
     | 
| 
       86 
     | 
    
         
            -
             
     | 
| 
       87 
     | 
    
         
            -
            extension FeedController {
         
     | 
| 
       88 
     | 
    
         
            -
             
     | 
| 
       89 
     | 
    
         
            -
                override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         
     | 
| 
       90 
     | 
    
         
            -
                    return tweets.count
         
     | 
| 
       91 
     | 
    
         
            -
                }
         
     | 
| 
       92 
     | 
    
         
            -
             
     | 
| 
       93 
     | 
    
         
            -
                override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         
     | 
| 
       94 
     | 
    
         
            -
                    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! TweetCell
         
     | 
| 
       95 
     | 
    
         
            -
                    cell.delegate = self
         
     | 
| 
       96 
     | 
    
         
            -
                    let tweetObject = tweets[indexPath.row]
         
     | 
| 
       97 
     | 
    
         
            -
                    cell.tweets = tweetObject
         
     | 
| 
       98 
     | 
    
         
            -
                    cell.captionLabel.text = tweetObject.caption
         
     | 
| 
       99 
     | 
    
         
            -
                    cell.infoLabel.attributedText = title
         
     | 
| 
       100 
     | 
    
         
            -
                    return cell
         
     | 
| 
       101 
     | 
    
         
            -
                }
         
     | 
| 
       102 
     | 
    
         
            -
             
     | 
| 
       103 
     | 
    
         
            -
                override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
         
     | 
| 
       104 
     | 
    
         
            -
                    let controller = TweetController(tweets: [tweets[indexPath.row]])
         
     | 
| 
       105 
     | 
    
         
            -
                    navigationController?.pushViewController(controller, animated: true)
         
     | 
| 
       106 
     | 
    
         
            -
                }
         
     | 
| 
       107 
     | 
    
         
            -
                
         
     | 
| 
       108 
     | 
    
         
            -
            }
         
     | 
| 
       109 
     | 
    
         
            -
             
     | 
| 
       110 
     | 
    
         
            -
            // MARK: - TweetCellDelegate
         
     | 
| 
       111 
     | 
    
         
            -
             
     | 
| 
       112 
     | 
    
         
            -
            extension FeedController: TweetCellDelegate {
         
     | 
| 
       113 
     | 
    
         
            -
                
         
     | 
| 
       114 
     | 
    
         
            -
                // 問題の関数
         
     | 
| 
       115 
     | 
    
         
            -
                func handleLikeTapped(_ cell: TweetCell) {
         
     | 
| 
       116 
     | 
    
         
            -
                    let indexPath = self.collectionView.indexPath(for: cell)
         
     | 
| 
       117 
     | 
    
         
            -
                    let tweetObject = tweets[indexPath!.row]
         
     | 
| 
       118 
     | 
    
         
            -
                    try! realm.write {
         
     | 
| 
       119 
     | 
    
         
            -
                        tweetObject.didLike.toggle()
         
     | 
| 
       120 
     | 
    
         
            -
                        if tweetObject.didLike {
         
     | 
| 
       121 
     | 
    
         
            -
                            cell.likeButton.tintColor = .lightGray
         
     | 
| 
       122 
     | 
    
         
            -
                            cell.likeButton.setImage(UIImage(named: "like_unselected"), for: .normal)
         
     | 
| 
       123 
     | 
    
         
            -
                            tweetObject.likes -= 1
         
     | 
| 
       124 
     | 
    
         
            -
                            realm.add(tweetObject, update: .all)
         
     | 
| 
       125 
     | 
    
         
            -
                        } else {
         
     | 
| 
       126 
     | 
    
         
            -
                            cell.likeButton.tintColor = .red
         
     | 
| 
       127 
     | 
    
         
            -
                            cell.likeButton.setImage(UIImage(named: "baseline_favorite_black_24pt_1x"), for: .normal)
         
     | 
| 
       128 
     | 
    
         
            -
                            tweetObject.likes += 1
         
     | 
| 
       129 
     | 
    
         
            -
                            realm.add(tweetObject, update: .all)
         
     | 
| 
       130 
     | 
    
         
            -
                        }
         
     | 
| 
       131 
     | 
    
         
            -
                    }
         
     | 
| 
       132 
     | 
    
         
            -
                }
         
     | 
| 
       133 
     | 
    
         
            -
            }
         
     | 
| 
       134 
     | 
    
         
            -
             
     | 
| 
       135 
     | 
    
         
            -
            public extension UIImage {
         
     | 
| 
       136 
     | 
    
         
            -
                func toPNGData() -> Data {
         
     | 
| 
       137 
     | 
    
         
            -
                    guard let data = self.pngData() else {
         
     | 
| 
       138 
     | 
    
         
            -
                        print("イメージをPNGデータに変換できませんでした。")
         
     | 
| 
       139 
     | 
    
         
            -
                        return Data()
         
     | 
| 
       140 
     | 
    
         
            -
                    }
         
     | 
| 
       141 
     | 
    
         
            -
                    return data as Data
         
     | 
| 
       142 
     | 
    
         
            -
                }
         
     | 
| 
       143 
     | 
    
         
            -
             
     | 
| 
       144 
     | 
    
         
            -
                func toJPEGData() -> Data {
         
     | 
| 
       145 
     | 
    
         
            -
                    guard let data = self.jpegData(compressionQuality: 1.0) else {
         
     | 
| 
       146 
     | 
    
         
            -
                        print("イメージをJPEGデータに変換できませんでした。")
         
     | 
| 
       147 
     | 
    
         
            -
                        return Data()
         
     | 
| 
       148 
     | 
    
         
            -
                    }
         
     | 
| 
       149 
     | 
    
         
            -
                    return data as Data
         
     | 
| 
       150 
     | 
    
         
            -
                }
         
     | 
| 
       151 
     | 
    
         
            -
             
     | 
| 
       152 
     | 
    
         
            -
            }
         
     | 
| 
       153 
     | 
    
         
            -
             
     | 
| 
       154 
     | 
    
         
            -
            ```
         
     | 
| 
       155 
     | 
    
         
            -
             
     | 
| 
       156 
     | 
    
         
            -
            * こちらがカスタムセル です。
         
     | 
| 
       157 
     | 
    
         
            -
            ```ここに言語を入力
         
     | 
| 
       158 
     | 
    
         
            -
            import UIKit
         
     | 
| 
       159 
     | 
    
         
            -
            import RealmSwift
         
     | 
| 
       160 
     | 
    
         
            -
            import ActiveLabel
         
     | 
| 
       161 
     | 
    
         
            -
             
     | 
| 
       162 
     | 
    
         
            -
            protocol TweetCellDelegate: class {
         
     | 
| 
       163 
     | 
    
         
            -
                func handleProfileImageTapped(_ cell: TweetCell)
         
     | 
| 
       164 
     | 
    
         
            -
                func handleReplyTapped(_ cell: TweetCell)
         
     | 
| 
       165 
     | 
    
         
            -
                func handleLikeTapped(_ cell: TweetCell)
         
     | 
| 
       166 
     | 
    
         
            -
                func deleteActionSheet(_ cell: TweetCell)
         
     | 
| 
       167 
     | 
    
         
            -
            }
         
     | 
| 
       168 
     | 
    
         
            -
             
     | 
| 
       169 
     | 
    
         
            -
            private let realm = try! Realm()
         
     | 
| 
       170 
     | 
    
         
            -
            private let userObject = Array(realm.objects(User.self))
         
     | 
| 
       171 
     | 
    
         
            -
            private let tweetObject = Array(realm.objects(Tweet.self))
         
     | 
| 
       172 
     | 
    
         
            -
             
     | 
| 
       173 
     | 
    
         
            -
            class TweetCell: UICollectionViewCell {
         
     | 
| 
       174 
     | 
    
         
            -
                
         
     | 
| 
       175 
     | 
    
         
            -
                // MARK: - Properties
         
     | 
| 
       176 
     | 
    
         
            -
                
         
     | 
| 
       177 
     | 
    
         
            -
                var tweets: Tweet = Tweet() {
         
     | 
| 
       178 
     | 
    
         
            -
                    didSet { configure() }
         
     | 
| 
       179 
     | 
    
         
            -
                }
         
     | 
| 
       180 
     | 
    
         
            -
                
         
     | 
| 
       181 
     | 
    
         
            -
                private var user: Results<User>!
         
     | 
| 
       182 
     | 
    
         
            -
                
         
     | 
| 
       183 
     | 
    
         
            -
                weak var delegate: TweetCellDelegate?
         
     | 
| 
       184 
     | 
    
         
            -
                
         
     | 
| 
       185 
     | 
    
         
            -
               
         
     | 
| 
       186 
     | 
    
         
            -
                public lazy var likeButton: UIButton = {
         
     | 
| 
       187 
     | 
    
         
            -
                    let button = UIButton(type: .system)
         
     | 
| 
       188 
     | 
    
         
            -
                    button.setImage(UIImage(named: "like_unselected"), for: .normal)
         
     | 
| 
       189 
     | 
    
         
            -
            //        button.tintColor = .darkGray
         
     | 
| 
       190 
     | 
    
         
            -
                    button.setDimensions(width: 20, height: 20)
         
     | 
| 
       191 
     | 
    
         
            -
                    button.addTarget(self, action: #selector(handleLikeTapped), for: .touchUpInside)
         
     | 
| 
       192 
     | 
    
         
            -
                    return button
         
     | 
| 
       193 
     | 
    
         
            -
                }()
         
     | 
| 
       194 
     | 
    
         
            -
               
         
     | 
| 
       195 
     | 
    
         
            -
                // MARK: - Selecter
         
     | 
| 
       196 
     | 
    
         
            -
                
         
     | 
| 
       197 
     | 
    
         
            -
                @objc func handleLikeTapped(){
         
     | 
| 
       198 
     | 
    
         
            -
                    delegate?.handleLikeTapped(self)
         
     | 
| 
       199 
     | 
    
         
            -
                }
         
     | 
| 
       200 
     | 
    
         
            -
                
         
     | 
| 
       201 
     | 
    
         
            -
                // MARK: - Helper
         
     | 
| 
       202 
     | 
    
         
            -
                
         
     | 
| 
       203 
     | 
    
         
            -
                public func configure(){
         
     | 
| 
       204 
     | 
    
         
            -
                    if userObject[0].profileImage != nil {
         
     | 
| 
       205 
     | 
    
         
            -
                        self.profileImageView.image = UIImage(data: userObject[0].profileImage!)
         
     | 
| 
       206 
     | 
    
         
            -
                    } else {
         
     | 
| 
       207 
     | 
    
         
            -
                        profileImageView.image = UIImage(named: "placeholderImg")
         
     | 
| 
       208 
     | 
    
         
            -
                    }
         
     | 
| 
       209 
     | 
    
         
            -
                }
         
     | 
| 
       210 
     | 
    
         
            -
                
         
     | 
| 
       211 
     | 
    
         
            -
                func createButton(withImageName imageName: String) -> UIButton {
         
     | 
| 
       212 
     | 
    
         
            -
                    let button = UIButton(type: .system)
         
     | 
| 
       213 
     | 
    
         
            -
                    button.setImage(UIImage(named: imageName), for: .normal)
         
     | 
| 
       214 
     | 
    
         
            -
                    button.tintColor = .darkGray
         
     | 
| 
       215 
     | 
    
         
            -
                    button.setDimensions(width: 20, height: 20)
         
     | 
| 
       216 
     | 
    
         
            -
                    return button
         
     | 
| 
       217 
     | 
    
         
            -
                }
         
     | 
| 
       218 
     | 
    
         
            -
                
         
     | 
| 
       219 
     | 
    
         
            -
                
         
     | 
| 
       220 
     | 
    
         
            -
                
         
     | 
| 
       221 
     | 
    
         
            -
            }
         
     | 
| 
       222 
     | 
    
         
            -
             
     | 
| 
       223 
     | 
    
         
            -
            ```
         
     |