繁体   English   中英

Swift 3-如何从Superview以及数组中删除所有子视图

[英]Swift 3 - How to remove all Subviews from Superview as well as Array

我目前有一个双for循环,可创建UIView CGRect正方形的X by Y网格。 该循环还将网格的每个UIView / Square添加到2D数组,使我可以访问网格的每个Cell并通过循环的索引值更改颜色/位置。

该循环似乎可以正常工作,并且可以完美地显示“单元格/方格”,但是过了一会儿我想删除所有正方形并清空数组(但不能完全删除),以便为新的下一个网格(可能是大小不同)。 我创建了一个从超级视图中删除所有视图的函数。

这就是我创建网格的每个“单元”并将其放入2D数组的方式:

let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)

let cell : UIView!
cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))

cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)

cell.tag = 100

self.view?.addSubview(cell)

gridArray[xPos][yPos] = cell

二维数组是在应用程序加载时创建的,如下所示:

gridArray = Array(repeating: Array(repeating: nil, count: gridSizeY), count: gridSizeX)

我试图寻找一种解决方案,以从超级视图中删除UIView,但是其他问题的所有答案似乎都不适合我。 我尝试将cell.tag = 100添加到每个UIView,然后删除所有Tag cell.tag = 100 UIView,例如:

for subview in (self.view?.subviews)! {
    if (subview.tag == 100) {
        subview.removeFromSuperview()
    }
}

但是没有运气。 我也尝试过使用这种方法:

self.view?.subviews.forEach({
    $0.removeConstraints($0.constraints)
    $0.removeFromSuperview()
})

与其他人的答案相比,我在代码中注意到的主要区别是我有“?” 和“!” 在代码中的位置。 我研究了它们的含义并理解了其中的大部分内容,但是我仍然不知道如何解决我的代码,因为如果有的话,感觉好像是问题所在。 我所知道的是,从超级视图中删除UIView的尝试不起作用,并且没有“?” 和“!”的代码根本不会运行。

如何为您正在使用的每个单元创建标签,例如:

    //Init value for for the tag.
    var n = 0

    func prepareCell() -> UIView {
             let xStart = Int(size.width/2/2) - ((gridSizeX * gridScale)/2)
             let yStart = Int(size.height/2/2) - ((gridSizeY * gridScale)/2)

         let cell : UIView!
         cell = UIView(frame: CGRect(x: xStart + (xPos * gridScale), y:yStart + (yPos * gridScale), width:gridScale, height:gridScale))

         cell.layer.borderWidth = 1
         cell.layer.borderColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgColor
         cell.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0)

        cell.tag = n

        //Each cell should have new value
        n += 1

        return cell
}

现在删除必需的视图。

func removeViews() {
    for z in 0...n {
        if let viewWithTag = self.view.viewWithTag(z) {
           viewWithTag.removeFromSuperview()
        }
        else {
           print("tag not found")
        }
    }
}

在操场上工作的示例:

var n = 0

let mainView = UIView()

func createView() -> UIView {
    let view = UIView()
    view.tag = n
    n += 1
    return view
}

for i in 0...16 {
    mainView.addSubview(createView())
}

func removeViews() {
    for z in 0...n {
        if let viewWithTag = mainView.viewWithTag(z) {
            viewWithTag.removeFromSuperview()
             print("removed")
        }
        else {
            print("tag not found")
        }
    }
}

您可能会忽略执行此操作的一种非常简单的方法...

在填充视图时,您已经构建了一个2D数组,其中包含对“单元”的引用。 因此,只需使用该数组将其删除即可。

// when you're ready to remove them
for subArray in gridArray {
    for cell in subArray {
        cell.removeFromSuperview()
    }
}

// clear out the Array
gridArray = Array<Array<UIView>>()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM