Swift 3 -如何从父视图和数组中删除所有子视图

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


I currently have a double for-loop that creates an X by Y grid of UIView CGRect squares. The loop also adds each UIView/Square of the grid to a 2D array allowing me to access each Cell of the grid and alter color/positioning by the index values of the loop.

我目前有一个双for循环,它创建UIView CGRect平方的X×Y网格。循环还将网格的每个UIView/Square添加到一个2D数组中,允许我访问网格的每个单元,并通过循环的索引值改变颜色/位置。

The loop seems to work fine and displays the Cells/Squares perfectly, however after a while I want to remove all of the squares and also empty the array (but not entirely delete) to make room for a new next grid (which may be of a different size). I created a function to remove all the views from the superview.

循环似乎工作得很好,并且完美地显示单元格/方块,但是过了一段时间,我想删除所有的方块,并清空数组(但不是完全删除),为新的下一个网格(可能大小不同)腾出空间。我创建了一个函数来从父视图中删除所有视图。

This is how I am creating each "Cell" of the grid and placing each into the 2D array:

这就是我如何创建网格的每个“单元格”,并将每个“单元格”放置到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

The 2D array is being created on application load like so:

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

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

I have tried to search for a solution to remove the UIViews from the superview however all answers from other Questions don't seem to work for me. I have tried to add cell.tag = 100 to each UIView and then remove all UIViews with the Tag Value of 100 like:

我试图寻找一种解决方案,将UIViews从父视图中移除,但是其他问题的所有答案似乎对我不起作用。我试过添加cell。标签= 100到每个UIView,然后删除所有UIView,标签值为100,比如:

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

However no luck. I have also tried to use this method:

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

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

The main differences I notice in my code compared to other peoples answers is that I have "?" and "!" at places in the code. I researched about what they mean and understood most of it however I still do not know how to fix my code because if it, and feel as though that is the issue. All I know is that the attempts to remove the UIViews from the superview does not work, and without the "?" and "!"s the code doesn't run at all.

我在代码中注意到的与其他人的答案的主要区别是我在代码中的某些地方有“?”和“!”。我研究了它们的含义,并理解了它们的大部分,但是我仍然不知道如何修复我的代码,因为如果是这样,我觉得这就是问题所在。我所知道的是,试图从超视图中删除uiview的尝试是无效的,并且没有“?”和“!”代码根本不运行。

2 个解决方案

#1


3  

How about to create tag for each cell you are using for example:

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

    //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
}

And now remove required views.

现在删除必需的视图。

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

Example that is working in the playground:

在操场上工作的例子:

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

#2


1  

You might be overlooking a much, much simpler way to do this...

你可能忽略了一个简单得多的方法。

You have built a 2D Array containing references to the "cells" as you populated your view. So, just use that Array to remove them.

在填充视图时,您已经构建了一个包含对“单元格”的引用的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>>()
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/04/07/7205cf013b1dc329db1d82deb555dfbd.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告