【问题标题】:UITableViewCell with a UIView subclass creates multiple layers on cell带有 UIView 子类的 UITableViewCell 在单元格上创建多个图层
【发布时间】:2015-11-05 01:02:14
【问题描述】:

我有一个使用 (.xib) 创建的自定义 UITableViewCell,并且在单元格中使用代码创建了 UIView 子类实例

表委托方法:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell
    let woView = ModuleView(frame: CGRect(x: 10, y: 10, width: 400, height: 400))
    woView.addBehavior(self.modulesForSubjects[indexPath.row])

    // remove subviews
    for subview in cell.subviews{
        subview.removeFromSuperview();
    }

    // then add your view
    cell.wiView = woView

    return cell
}

我的完整 UIView 子类:

class ModuleView: UIView {
override init (frame : CGRect) {
    super.init(frame : frame)
}

convenience init () {
    self.init(frame:CGRect.zero)
}

required init(coder aDecoder: NSCoder) {
    fatalError("This class does not support NSCoding")
}


func addBehavior (ArrayOfmodulesForSubject: [SubjectsModules]) {
    var number :CGFloat = 30
    var number2 :CGFloat = 30

    print(ArrayOfmodulesForSubject.count)
    for  (i, index) in ArrayOfmodulesForSubject.enumerate()  {

    let yourLabel1 = UILabel(frame: CGRectMake(number, 40 , 35 , 30))
    let yourLabel2 = UILabel(frame: CGRectMake(number2, 60 , 140 , 30))
    yourLabel1.textColor = UIColor.whiteColor()
    yourLabel1.backgroundColor = UIColor.redColor()
    yourLabel1.text = String(index.score)

    yourLabel2.textColor = UIColor.whiteColor()
    yourLabel2.backgroundColor = UIColor.blackColor()
    yourLabel2.text = index.date

    self.addSubview(yourLabel1)
    self.addSubview(yourLabel2)
    number += 30
    number2 += 70
    }


}

这是我在滚动后得到的:

UPDATE rnsjtngus 解决方案,不幸的是也没有帮助(也许我做错了什么)

class CustomOneCell: UITableViewCell {
var arr  = [SubjectsModules]()
@IBOutlet weak var wiView: ModuleView!
override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?){

    // Initialize your custom cell

    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.setupWoView(self.arr)
}
override func awakeFromNib() {

    print("AWAKED FROM NIB")
    for subview in subviews{
        subview.removeFromSuperview()
        if superview != nil {
        superview!.removeFromSuperview()
        }
    }

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

func setupWoView(arrayOfmodulesForSubject: [SubjectsModules]) {
    let woView = ModuleView(frame: CGRect(x: 10, y: 10, width: 400, height: 400))
    woView.backgroundColor = UIColor.blueColor()
    self.addSubview(woView)
}

}

【问题讨论】:

  • 尝试将woView 初始化移动到单元格子类,而不是添加为单元格的子视图。

标签: swift uitableview uiview


【解决方案1】:

简单但不好的解决方案是 在添加自己的视图之前从单元格中删除子视图

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell
    let woView = ModuleView(frame: CGRect(x: 10, y: 10, width: 400, height: 400))
    woView.addBehavior(self.modulesForSubjects[indexPath.row])

    // remove subviews
    for subview in cell.subviews{
       subview.removeFromSuperview();
    }

    // then add your view
    cell.addSubview(woView)

    return cell
}

【讨论】:

  • 如果你想留下一些子视图,只需在调用'removeFromSuperview()'函数之前检查子视图。
  • 很好的方法,但错误仍然存​​在 :( 事实上没有任何改变
  • 你制作了自己的 TableViewCell.swift 文件吗?在 TableViewCell 文件中执行一些 removeFromSuperview() 操作。
  • 例如awakeFromNib()函数中,先删除子视图,第二个和自己的视图。
  • 如果我根据您的回答正确地做所有事情,请检查更新后的内容(现在多层仍然在这里)
【解决方案2】:

当您调用 dequeue 时,如果现有单元格可用,该方法会将现有单元格出列;如果没有,则使用类或 nib 创建新单元格。

这意味着您很可能会得到一个已经添加了子视图的视图,当您再次将其出列时,您的代码会添加第二个。您需要首先删除任何现有的子视图。

【讨论】:

  • 我已经尝试了很多删除现有子视图的方法,但都没有成功:(
猜你喜欢
  • 1970-01-01
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 2018-05-26
  • 2018-03-23
  • 1970-01-01
  • 2015-01-06
相关资源
最近更新 更多