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
最後的標籤仍然在我的視圖上。
任何想法?
我們已加入到我的刪除功能,但仍然是相同的。 self.containerView.removeFromSuperview() –
請更新您的代碼。 – dichen
我剛更新了它。加上update()只被調用一次。這是我的所有代碼的方式... –