2016-03-22 43 views
0

我在一個UIScrollView添加一些標籤,正從一個的NSMutableArray自己的文字:從UIView的斯威夫特刪除的UILabel 2

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

我沒有看到你清晰self.containerView電話self.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) 


} 
+0

我們已加入到我的刪除功能,但仍然是相同的。 self.containerView.removeFromSuperview() –

+0

請更新您的代碼。 – dichen

+0

我剛更新了它。加上update()只被調用一次。這是我的所有代碼的方式... –