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

注意!

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



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