2017-06-26 8 views
1

私のUITableViewCellの再利用可能な側面に問題があります。私は、異なるタイプのデータを異なる細胞に送り込んでいます。データの種類が同じ場合(例:画像のみ)、新しい画像が古い画像を上書きするため、問題はありません。しかし、データのタイプが同じであれば、両方とも固執しています。UITableViewCellコンテンツを削除する

私はセルを完全に消去するための方法やツールを探しています。私は辞書などの辞書を使って直接データを参照して消去しようとしましたが、何が再利用されたセルに何のデータがあるのか​​を一貫して伝えられないために失敗しました。その他のものは

cell.contentView.subviews.removeAll() 

エラーを投げた、他には

cell.contentView.removeFromSuperView() 

だったが、それはただ、これまでロードセルが使用可能と何もしました。

私のコードは以下のとおりです。助けを進めていただきありがとうございます(日付のものは無視してください)。

if dictionary?["type"]! != nil { 


       cell.imageView?.layer.borderColor = UIColor.black.cgColor 
       cell.layer.borderWidth = 1 
       cell.imageView?.layer.cornerRadius = 1 


       cell.imageView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height/2, width: 25, height: 25) 

       self.tableTextOverLays(i: indexPath.row, type: Int((dictionary?["type"])! as! NSNumber), cell: cell) 

       if (indexPath.row == tableFeedCount - 1) && lockoutFeed != true && tableFeedCount == self.allDictionary.count && imageFeedCount == imageDictionary.count { 
        let dictionary = self.allDictionary[indexPath.row] 
        let date = dictionary?["date"] as! Double 
        let date2 = date*100000 
        let date3 = pow(10,16) - date2 
        let date4 = String(format: "%.0f", date3) 
        self.setUpFeed(starting: date4, queryLimit: UInt(11)) 
       } 


      } 

      else if (dictionary?["type_"] as! String) == "Image" { 
       print("image feed", indexPath.row) 



       cell.imageView?.layer.borderColor = UIColor.black.cgColor 

       cell.layer.borderWidth = 1 
       cell.imageView?.layer.cornerRadius = 1 
       cell.imageView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height/2, width: 25, height: 25) 
       cell.imageView?.image = imageDictionary[indexPath.row] 

       if indexPath.row == tableFeedCount - 1 && lockoutFeed != true && tableFeedCount == self.allDictionary.count && imageFeedCount == imageDictionary.count{ 
        let dictionary = self.allDictionary[indexPath.row] 
        let date = dictionary?["date"] as! Double 
        let date2 = date*100000 
        let date3 = pow(10,16) - date2 
        let date4 = String(format: "%.0f", date3) 

        setUpFeed(starting: date4, queryLimit: UInt(11)) 
       } 


      } 
      else if (dictionary?["type_"] as! String) == "Video" { 
       print("video feed", indexPath.row) 



       cell.imageView?.layer.borderColor = UIColor.black.cgColor 

       cell.layer.borderWidth = 1 

       let imagerView = UIImageView() 
       imagerView.layer.cornerRadius = 1 
       imagerView.frame = CGRect(x: 0, y: 0, width: 200, height: 200) 
       imagerView.image = videoDictionary[indexPath.row] 

//    let vidUIView = UIView() 
//    vidUIView.tag = indexPath.row 

       let asf = UITapGestureRecognizer(target: self, action: #selector(ProfileController.playVid2(gestureRecognizer:))) 
       asf.numberOfTapsRequired = 2 
//    imagerView.addGestureRecognizer(asf) 


       let adsf = UITapGestureRecognizer(target: self, action: #selector(ProfileController.playVid(gestureRecognizer:))) 
       adsf.numberOfTapsRequired = 1 
       adsf.require(toFail: asf)     
//    imagerView.addGestureRecognizer(adsf) 

       cell.contentView.addGestureRecognizer(asf) 
       cell.contentView.addGestureRecognizer(adsf) 

       cell.contentView.frame = CGRect(x: 0, y: 0, width: 200, height: 200) 

       cell.contentView.addSubview(imagerView) 

       //cell.contentView.addSubview(vidUIView) 


       if indexPath.row == tableFeedCount - 1 && lockoutFeed != true && tableFeedCount == self.allDictionary.count && videoFeedCount == videoDictionary.count{ 
        let dictionary = self.allDictionary[indexPath.row] 
        let date = dictionary?["date"] as! Double 
        let date2 = date*100000 
        let date3 = pow(10,16) - date2 
        let date4 = String(format: "%.0f", date3) 

        setUpFeed(starting: date4, queryLimit: UInt(11)) 
       } 

      } 
+0

このテーブルビューでデリゲートパターンを使用していますか?そうでない場合、あなたはそれをチェックしたいかもしれません。それはかなり滑らかです。 https://developer.apple.com/documentation/uikit/uitableviewdelegate –

+0

答える時間をとってくれてありがとう、私はuitableviewdelegateを使用しています。私が助けが必要なのは、テーブルを使う方法よりも進んでいます。 – Blue

答えて

2

そのコードがビューコントローラまたはUITableViewCellサブクラスにあるかどうかは不明です。いずれにせよ、あなたはあなたの細胞のためのUITableViewCellのサブクラスを使用してprepareForReuseの清掃作業を行うことになるでしょう:

override func prepareForReuse() { 
    super.prepareForReuse() 

    imagerView.image = nil 
} 

これは、あなたがたときにセルに引き継がれたくないセルから任意のデータを削除する伴いますそれは再利用されます。画像ビューから画像を消去する、すべてのセルタイプで表示されないビューを隠すなどの操作を行うことができます。

もう1つの解決策は、表示するデータタイプに基づいて複数のセルクラスを読み込み、 。これは上記のコードのいくつかを単純化し、再利用を防ぐのに役立つかもしれませんが、見た目ではかなりのリファクタが必要になります。

+0

ありがとうございます。私は、テーブルに画像がいっぱいで、あなたの答えがすべて同じデータ(画像)を持つテーブルの場合、問題はないと明言しています。あなたが解決している問題が私が持っているものではないことを考えれば、それがどのように役立つのか分かりません。また、私のコードでは、imagerViewは上記の関数のスコープ内で作成されているので、関数の外でそれを参照することは再び無意味になります。 – Blue

+0

@AaronMannスコープ内でイメージビューを作成することは問題です。私はすべての場合に古いイメージが残っていると思われますが、イメージビューセルを別のイメージビューセルに置き換えると、イメージビューを重ねてスタックするだけで、古いものがまだそこにあることはわかりません。スコープ外の参照を保持する必要があります。あなたが提供したコードで何が起こっているのかを知るのはかなり難しいです。通常、異なるデータ型ごとに異なるセルクラスを使用します。これらのセルタイプはそれぞれ 'prepareForReuse'でクリーンアップを行います。 – skladek

関連する問題