我有一个用(.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
    }


}

以下是我在滚动之后得到的信息:
swift - 带有UIView子类的UITableViewCell在单元格上创建多个图层-LMLPHP
更新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)
}

}

最佳答案

很简单,但坏的解决方案是
在添加自己的视图之前,只需从单元格中删除子视图

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
}

关于swift - 带有UIView子类的UITableViewCell在单元格上创建多个图层,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33534938/

10-10 09:02