2017-08-04 34 views
0

我有一个UITableView包含2个部分。在第一部分中,我不想让我创建的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中添加子视图时,每次都不会删除子视图。因此,如果由于滚动打开和关闭而出现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

在你的单元格中创建一个公共变量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

这将是很好做,因为:

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

当使用

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

“如果”你应该利用每一个与它的“其他”条款的条件,当你关闭它重用滚动作为导致它重新出现的单元格。所以,如果你不写其他条件,并且如果条件成为假,它将使用旧的单元格数据。