【问题标题】:Remove subviews from ScrollView Swift从 ScrollView Swift 中删除子视图
【发布时间】:2014-11-21 15:29:17
【问题描述】:

我使用 for 循环在我的滚动视图中创建标签和按钮。 是否可以删除我的滚动视图内的所有对象? (我想用新内容更新它)

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()
}

【问题讨论】:

    标签: ios swift uiscrollview


    【解决方案1】:

    你试过了吗?

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

    【讨论】:

    • 谢谢! :) 我正在寻找 removeFromSuperview()!像魅力一样工作!
    • 这对我有用,如果只想删除最后添加的子视图:let subViews = self.view.subviewssubViews.last?.removeFromSuperview()
    【解决方案2】:

    一线解决方案,使用

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

    更新

    对于仅删除特定类型的视图,例如 UIButton 使用

    scrollView.subviews.forEach ({ ($0 as? UIButton)?.removeFromSuperview() })
    

    【讨论】:

    • 我如何判断一种类(如:UIButton)以这种方式删除?
    【解决方案3】:

    可以使用标签从不同的类中删除一组对象。在创建对象集时设置标签。

    label.tag = 99
    

    现在,当移除对象时,使用:

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

    【讨论】:

      【解决方案4】:

      你可以用块方法做到这一点,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 2011-01-25
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        相关资源
        最近更新 更多