从UIView Swift 2中删除UILabel

时间:2016-03-22 23:21:21

标签: ios swift2 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时删除数组中的最后一个对象,但最后一个标签仍在我的视图中。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在致电self.containerView之前,我没有看到您self.update()。我打赌你多次调用更新,并且在同一个地方有几个标签。

在update()的开头尝试清除containerView:

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)


}