2016-03-22 5 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.update()の前にself.containerViewをクリアしていません。私はあなたが複数回更新を呼び出すと、同じ場所にいくつかのラベルがあることを賭けた。

)は(更新の先頭に明確な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

私は私のremove関数にまだ同じことを追加しました。 self.containerView.removeFromSuperview() –

+0

コードを更新してください。 – dichen

+0

私はちょうどそれを更新しました。 + update()は一度だけ呼び出されます。これは途中で私のすべてのコードです... –

関連する問題