2016-05-23 16 views
11

私は、ユーザーのお気に入りの投稿に応じてトグルされるボタン付きのUITableViewを持っています。テーブルビューがスクロールされるときを除いて、すべてがうまくいきます。ボタンが変わります。ここに私のコードは次のとおりです。スクロールでの値の変更UITableView

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    guard let feed = self.feed else { 
     return 0 
    } 
    return feed.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if feed!.count > 9 { 
     if indexPath.row == feed!.count - 1 { 
      self.loadMorePosts() 
     } 
    } 

    if hasImageAtIndexPath(indexPath) { 
     return imageCellAtIndexPath(indexPath) 

    } else { 
     return basicCellAtIndexPath(indexPath) 
    } 

} 

func hasImageAtIndexPath(indexPath:NSIndexPath) -> Bool { 
    let post = self.feed?[indexPath.row] 

    if post?.image?.isEmpty == false { 
     return true 
    } 

    return false 
} 

func imageCellAtIndexPath(indexPath:NSIndexPath) -> PostCellImage { 
    let cell:PostCellImage = self.tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! PostCellImage 


    if let post = self.feed?[indexPath.row] { 
     let likedPost = post.hasFavorited 

     if likedPost == true { 
      if let favoriteCount = post.favoriteCount { 
       let count = String(favoriteCount) 
       cell.likeButton.setTitle(count, forState: .Normal) 
       cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal) 
       cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal) 
       cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside) 
       cell.likeButton.tag = post.id! 
      } 
     } else { 
      if let favoriteCount = post.favoriteCount { 
       let count = String(favoriteCount) 
       cell.likeButton.setTitle(count, forState: .Normal) 
       cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside) 
       cell.likeButton.tag = post.id! 
      } 
     } 
    } 

    return cell 
} 

お気に入り投稿アレイ

var favoritedPosts = [Int]() 

テーブルビュー

if let likedPost = post.hasFavorited { 
    if likedPost == true { 
     self.favoritedPosts.append(indexPath.row)    
     print(self.favoritedPosts) 
    } 
} 

if self.favoritedPosts.contains(indexPath.row) { 
    let count = String(post.favoriteCount) 
    cell.likeButton.setTitle(count, forState: .Normal) 
    cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal) 
    cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal) 
    cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside) 
    cell.likeButton.tag = post.id!  
} else { 
    let count = String(post.favoriteCount) 
    cell.likeButton.setTitle(count, forState: .Normal) 
    cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside) 
    cell.likeButton.tag = post.id!   
} 
+0

これは、多くの場合、誰もが1〜2回いると思う小さな落とし穴です。 'dequeReusableCells'メソッドを使用していると仮定します。スクロールすると、効率的な理由から、そのメソッドは既にテーブルにあるがウィンドウの外側にあるセルを吐き出し続けます。条件を使用していくつかのUI要素を設定する場合、セル内のすべての要素に値を指定しないと、セルの古い値が引き続き使用され、意図しない結果が返されます。私は100%あなたの問題だと確信していません。あなたの質問に 'cellForRowAtIndexPath'メソッド全体を含めることをお勧めします。 –

+0

ちょうどコード –

+0

を追加すると、セルの再利用に起因する問題を避けるために、ネストされた 'if'ステートメントに' else'パーツを追加するべきです。これを試してください。 –

答えて

8

UITableViewCellPostCellImageサブクラスでは、prepeareForReuse関数をオーバーライドする必要があります。つまり、セルをデフォルトモードにすることです。

スウィフト:

override func prepareForReuse() { 
    super.prepareForReuse() 

    //set cell to initial state here 
    //set like button to initial state - title, font, color, etc. 
} 
+0

これは私の問題を解決したものです。 –

+0

素晴らしいです、私は助けてうれしいです。 –

5

これは、テーブルビューセルの再利用により、ケースに入れている可能性があります。ポストがお気に入りの投稿であるにもかかわらず投稿が好きな投稿でないときに画像を削除しなかった場合、あなたのコードはlikeButton.imageを設定することが判明しました。だから、初めて各セルがtableViewにロードされるときに、すべてのことがうまく動作します。しかし、tableViewをスクロールするときに、お気に入りの画像セットを持つセルが画面領域外に移動すると、それらはセルスクロールのために再利用されます。したがって、この種のセルが好きではない投稿でも再利用されると、まだそこにいるだろう。

UITableViewCellのprepareForReuseメソッドがあります。これは、セルが再利用される前に内容をリセットする機会を与えます。

+0

prepareForReuseメソッドが見つかりません...他に誰もこの問題がありますか?おそらくこれが、質問の賞金が「現在の回答は古いものであり、最近の変更があれば修正が必要だ」と述べているのかもしれません。 – penatheboss

+0

@penatheboss:このメソッドは、UITableViewCellメソッドのメソッドです。 UITableViewCellサブクラスでそれをオーバーライドします。 – truongky

+0

@truongkyああ!それは理にかなっています。ありがとう! – penatheboss

0

あなたはあなたがここでは完全に正常なケースの外観をリセットする必要がありprepareForReuseを試したりする場合にはlikedPost == false

0
  • をとは異なり、画像を設定してみてください。
  • 理由は、 セルが再利用されたときに取得するボタンの状態を確認できないことです。
  • デキューされたセルを受信すると、「好き」/「好きではない」状態の ボタンが表示されることがあります。
  • else句に2つのコメントアウトされた行の外観を完成させると、問題が解決されます。

    if self.favoritedPosts.contains(indexPath.row) { 
        let count = String(post.favoriteCount) 
        cell.likeButton.setTitle(count, forState: .Normal) 
        cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal) 
        cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal) 
        cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside) 
        cell.likeButton.tag = post.id! 
    } else { 
        let count = String(post.favoriteCount) 
        cell.likeButton.setTitle(count, forState: .Normal) 
    
        // Uncomment these two lines and add proper values for image/color to resolve your problem 
        // cell.likeButton.setImage(UIImage(named: "not-liked-yet"), forState: .Normal) 
        // cell.likeButton.setTitleColor(UIColorFromRGB("A67832"), forState: .Normal) 
    
        cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside) 
        cell.likeButton.tag = post.id!   
    } 
    

この情報がお役に立てば幸いです。

+0

何らかの理由で、私はこれらの2行をコメントアウトしても同じ問題を抱えています。 –

+0

@AlexSmithあなたはそれらをコメントアウトしてはなりません。それらはあなたの問題の解決策です。私の最後の点を注意深くお読みください。 ** else節で2つのコメントアウトされた行を使って外観を完成させると、あなたの問題は解決されます**ここでイメージと色のための普通の(そうでない)大文字小文字の値を指定する必要があります。 –

+0

@AlexSmith私はちょうど私の答えを再フォーマットしました。それを見てみましょう。これは私が何を意味するかを明確にするはずです。 –

関連する問題