2014-11-21 17 views
5

से सबव्यू हटाएं मैं अपने स्क्रॉल व्यू के अंदर लेबल और बटन बनाने के लिए फॉर-लूप का उपयोग करता हूं। क्या मेरे स्क्रॉल व्यू के बाहर सभी ऑब्जेक्ट्स को हटाना संभव है? (मैं इसे नई सामग्री के साथ अपडेट करना चाहता हूं)स्क्रॉलव्यू स्विफ्ट

for peop in personArray{ 

     scrollView.clearContent ?????? 


     // Name label 
     var label: UILabel = UILabel() 
     label.frame = CGRectMake(8, CGFloat(nameHeight), 183, 21) 
     label.backgroundColor = UIColor.whiteColor() 
     label.textColor = UIColor(red: 90/255.0, green: 187/255.0, blue: 206/255.0, alpha: 1.0) 
     label.textAlignment = NSTextAlignment.Left 
     label.font = UIFont (name: "HelveticaNeue-Light", size: 14) 
     label.text = " \(peop.getName()) - \(sex)" 
     self.scrollView.addSubview(label) 


     //Delete button 
     var button = UIButton.buttonWithType(UIButtonType.System) as UIButton 
     button.tag = playerId 
     button.frame = CGRectMake(199, CGFloat(nameHeight), 37, 21) 
     button.backgroundColor = colorWheel.colorsArray[7] 
     button.setTitle("Slet", forState: UIControlState.Normal) 
     button.addTarget(self, action: "delAction:", forControlEvents: UIControlEvents.TouchUpInside) 
     button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) 
     self.scrollView.addSubview(button) 
     button.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 14) 


     scrollHeight = scrollHeight + 29 
     nameHeight = nameHeight + 29 
     playerId++ 
    } 
    scrollView.contentSize = CGSize(width: 20.0, height: CGFloat(nameHeight)) 
} 

func delAction(sender: UIButton!){ 
    personArray.removeAtIndex(sender.tag) 
    updatePeople() 
} 

उत्तर

18

क्या आपने यह कोशिश की है?

let subViews = self.scrollView.subviews 
for subview in subViews{ 
    subview.removeFromSuperview() 
} 
+0

धन्यवाद! :) मैं हटाने के लिए देख रहा था FromSuperview()! एक जादू की तरह काम करता है! – Heinevolder

+1

यह मेरे लिए काम करता है, अगर केवल अंतिम जोड़ा गया सबव्यूव निकालना है: 'subViews = self.view.subviews' 'subViews.last? .removeFromSuperview()' – Andrej

1

आप

let views: NSArray = scroller.subviews 

// 3 - remove all subviews 
views.enumerateObjectsUsingBlock { 
(object: AnyObject!, idx: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 
    object.removeFromSuperview() 
} 
15

एक पंक्ति समाधान ब्लॉक दृष्टिकोण के साथ ऐसा कर सकते हैं, का उपयोग करें,

scrollView.subviews.forEach({ $0.removeFromSuperview() }) 

UPDATED

देखने का केवल विशेष प्रकार को हटाने के लिए, मान लीजिए UIButton

का उपयोग करें
scrollView.subviews.forEach ({ ($0 as? UIButton)?.removeFromSuperview() }) 
+0

मैं कैसे एक प्रकार की कक्षा का न्याय कर सकता हूं (जैसे: UIButton) इस तरह से हटाने के लिए? –

+0

@WilliamHu मेरा संपादित उत्तर देखें। – itsji10dra

1

अलग-अलग वर्गों से वस्तुओं का एक सेट निकालना टैग के साथ किया जा सकता है। वस्तुओं का सेट बनाते समय टैग सेट करें।

label.tag = 99 

अब, जब तत्वों को हटा, का उपयोग करें:

func removeLabels() { 
    let subViews = self.view.subviews 
    for subview in subViews { 
     if subview.tag == 99 { 
      subview.removeFromSuperview() 
     } 
    } 
}