带有UIView子类的UITableViewCell在单元格上创建多个图层

时间:2015-11-05 01:02:15

标签: swift uitableview uiview

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


}

这是我在滚动后得到的结果:

multiple layers of labels

更新 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)
}

}

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

当您调用dequeue时,如果现有单元格可用,则该方法会将其取列,或者如果不是则使用该类或nib创建新单元格。

这意味着您可能会得到一个已经添加了子视图的内容,当您再次将其取消时,您的代码将添加第二个。您需要先删除所有现有的子视图。