从 UIView Swift 2 中删除 UILabel



我在UIScrollView上添加了一些标签,并从NSMutableArray获取它们的文本:

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var myScrollView: UIScrollView!
    let containerView = UIView()
    var array = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
       // containerView.frame = myScrollView.frame
         array = ["some text", "some other text", "text 3and 4 loremnd 4 lorem ips ipsum", "more text", "Lorem ipsum dolor sit amet", "consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam", "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in", "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident", "sunt in culpa qui officia deserunt mollit anim id est laborum"]
        self.update()

    }

    @IBAction func addSomething(sender: AnyObject) {
        array.addObject("this is at the end")
       self.update()
    }
    @IBAction func removeSomething(sender: AnyObject) {
        self.containerView.removeFromSuperview() //this doesn't work
        array.removeLastObject()
         self.update()
    }

    func update() {

        for i  in 0...array.count - 1 {
            let label = UILabel(frame: CGRectMake(10, CGFloat(i) * 74 + 20, 64, 64))
            label.text = array[i] as? String
            label.textAlignment = NSTextAlignment.Center
            label.numberOfLines = 0
            label.lineBreakMode = NSLineBreakMode.ByWordWrapping
            label.font = UIFont.systemFontOfSize(10)
            label.adjustsFontSizeToFitWidth = true
            label.minimumScaleFactor = 0.4
            label.layer.masksToBounds = true
            label.layer.cornerRadius = 32
            label.layer.borderColor = UIColor.brownColor().CGColor
            label.layer.borderWidth = 1.0
            self.containerView.addSubview(label)

        }
        self.myScrollView.contentSize.height = CGFloat(array.count) * 78
        myScrollView.addSubview(containerView)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

我的问题是,尽管我可以在调用removeSomething function时从数组中删除最后一个对象,但最后一个标签仍在我的视图中。

有什么想法吗?

我没有看到你在打电话self.update()之前清除self.containerView. 我敢打赌你多次调用更新,并且在同一位置有几个标签。

尝试在 update() 开始时清除容器视图:

func update() {
    containerView.removeFromSuperview() // Clear containerView
    containerView = UIView() // Create a new instance
    for i  in 0...array.count - 1 {
        let label = UILabel(frame: CGRectMake(10, CGFloat(i) * 74 + 20, 64, 64))
        label.text = array[i] as? String
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping
        label.font = UIFont.systemFontOfSize(10)
        label.adjustsFontSizeToFitWidth = true
        label.minimumScaleFactor = 0.4
        label.layer.masksToBounds = true
        label.layer.cornerRadius = 32
        label.layer.borderColor = UIColor.brownColor().CGColor
        label.layer.borderWidth = 1.0
        self.containerView.addSubview(label)

    }
    self.myScrollView.contentSize.height = CGFloat(array.count) * 78
    myScrollView.addSubview(containerView)

}

最新更新