我已经设置了一个简单的演示来解释我的问题。我有2个功能。 1加载一个红色的UIView,一个删除当前的UIView并用一个新的替换它在不同的X位置。我知道我可以改变当前UIView的x位置,但这不是重点。
我还需要一个数组来存储UIView,我正在创建它:
var cellArray = Array<UIView>()
加载UIView的第一个函数如下:
func loadGridCell(xPos:Int) {
let cell = UIView(frame: CGRect(x: xPos, y: 20, width: 20, height: 20))
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:0.90).cgColor
cell.backgroundColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:0.5)
self.view?.addSubview(cell)
cellArray.append(cell)
print("Spawning Cell")
}
此功能完美运行并创建红色UIView。第二个功能如下:
func loadNextCell() {
print("Removing Current Cell")
for element in cellArray {
element.removeFromSuperview()
}
cellArray.removeAll()
print("Creating New Cell")
loadGridCell(xPos: 40) //This should create a new UIView with an x-position of 40
}
此功能也正常工作,直到它尝试再次使用不同的X位置调用第一个函数。它删除了第一个UIView非常好,但似乎并不想创建下一个。最奇怪的部分是,函数被调用,文本&#34;产生细胞&#34;正在显示,它告诉我新的UIView正在创建,但有些东西阻止它被显示。
可能导致这种情况的原因是什么?