2017-08-04 13 views
0

2つのセクションを持つUITableViewがあります。最初のセクションでは、私は左に表示するように作成したUIViewを望んでいません。それは最初にロードするときにうまく動作しますが、画面から離れて再びオンになると再び表示されます。スクロールした後にUITableViewCellで表示が再現される

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! ATableCell; 
    cell.delegate = self; 

    if (indexPath.section == 1) 
    { 
     // let height = cell.bounds.size.height; 
     let height = 100; 

     let turnsView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: height)); 
     turnsView.backgroundColor = UIColor.purple; 

     cell.addSubview(turnsView); 
    } 
    else 
    { 
     let height = 100; 

     let turnsView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: height)); 
     turnsView.backgroundColor = UIColor.clear; 

     cell.addSubview(turnsView); 
    } 

    // Configure the cell... 

    cell.backgroundColor = UIColor.clear; 
    cell.textLabel?.text = "texting"; 
    cell.detailTextLabel?.text = "testing"; 
} 

最初のセクションに紫色のビューが表示されないようにします。

+0

ビューにタグを割り当て、タグを作成する前にif-letステートメントを使用して、それらがセルに既に存在する場合は削除します。スクロールが始まると、自分のセルにuilabelsが再び現れていたのと同様の問題がありました。 – Rishabh

答えて

0

cellForRowのセルにサブビューを追加しないでください。セルが再利用されていることを理解する必要があり、cellForRowにsubViewを追加すると、毎回サブビューが削除されません。したがって、スクロールのオンとオフのためにセルが10回表示された場合は、再利用されたセルに10個のサブビューが追加されます。

あなたの場合、同じことが起こっています。 XIB/Prototypeセルでビューを追加することをお勧めします。参照を与え、cellForRowのbackgroundColorプロパティを変更します。

または、XIBまたはプロトタイプセルを使用していない場合は、セルクラスのinitメソッドでサブビューを追加します。

var turnsView = UIView() 

override init(style: UITableViewCellStyle, reuseIdentifier: String!) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 
    turnsView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 100)) 
    self.contentView.addSubview(turnsView) 

} 
+0

セルのサブクラスでawakeFromNibでビューを作成します。それをタグとして設定します。次にcellForRowで背景色を変更しますか?正しい?そうだといい。 – cdub

+0

セル内にpublic変数turnsViewを作成します。それを作成してawakeFromNibに追加します。次に、cellForRowでプロパティを設定します。turnsView.backgroundColor = .purple –

+0

nib/Storyboardプロトタイプのセルを使用している場合は、そこにビューを追加して参照することをお勧めします。そうでない場合は、答えに記載されているようにinitメソッドで追加します。 –

0

これはあなたのために働く必要があります。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! ATableCell; 
    cell.delegate = self; 

    //Remove subviews before adding again 
    if let purpleV = cell.viewWithTag(1) { 
     purpleV.removeFromSuperview() 
    } 
    if let purpleV = cell.viewWithTag(2) { 
     purpleV.removeFromSuperview() 
    } 

    if (indexPath.section == 1) 
    { 
     // let height = cell.bounds.size.height; 
     let height = 100; 

     let turnsView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: height)); 
     turnsView.tag = 1 
     turnsView.backgroundColor = UIColor.purple; 

     cell.addSubview(turnsView); 
    } 
    else 
    { 
     let height = 100; 

     let turnsView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: height)); 
     turnsView.tag = 2 
     turnsView.backgroundColor = UIColor.clear; 

     cell.addSubview(turnsView); 
    } 

    // Configure the cell... 

    cell.backgroundColor = UIColor.clear; 
    cell.textLabel?.text = "texting"; 
    cell.detailTextLabel?.text = "testing"; 
} 

明確にするdequeuereusablecellメソッドのドキュメントをお読みください。

+0

これは動作しますが、良い解決策ではありません。 cellForRowのサブビューの追加と削除は、スクロール中にジャークを発生させます(スクロールが滑らかではありません)。常にそれを避けてください。 –

+0

私は同意する、良い解決策ではないが、私はどんな躍動感も経験しない、スクロールはスムーズです。 – Rishabh

0

としてこれを行うことが良いだろう。

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

を使用しているときに、それは再利用するオフあなたがスクロールするときのようにその「他の」句を使用して、すべての「if」の条件を使用する必要があります

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) 
    // Config the cell... 
    if let turnsCell as? ATableCell { 
     turnsCell.turnsColor = (indexPath.section == 1 ? UIColor.purple : UIColor.clear) 
    } 

    cell.textLabel?.text = "texting" 
    cell.detailTextLabel?.text = "testing" 

    return cell 
} 

class ATableCell: UITableViewCell { 
    var turnsView = UIView() 

    var turnsColor: UIColor { 
     get { 
      return self.turnsView.backgroundColor 
     } 
     set { 
      self.turnsView.backgroundColor = newValue 
     } 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // let height = self.bounds.size.height; 
     let height = 100; 

     turnsView.frame = CGRect(x: 0, y: 0, width: 10, height: height) 
     self.contentView.addSubview(turnsView) 
     self.backgroundColor = UIColor.clear 
    } 
} 
1

それを再現させるセル。したがって、else条件を記述しないと、条件がfalseになると古いセルデータが使用されます。

関連する問題