即使清除数组,iOS UITextViews数据仍会挥之不去

时间:2019-04-19 11:52:46

标签: ios arrays swift uiviewcontroller uitextview

下图显示了UIViewController的Xcode图形调试层次结构。看来我需要销毁UITextViews数组中编辑后正在重新创建的UITextViews的其他数据。每次进行更改时,我都将UITextViews的数组设置为[],然后使用更新的数据重新创建它。即使我确认UITextView数组确实已重置为零个元素,然后以预期的元素数重新创建,这些幻影图像仍在屏幕上徘徊,并且调试层次结构显示出某些内容未被删除。

我怀疑我需要做一些整理工作以查找和销毁与UITextViews相关的其他数据,并且将数组设置为零并不能清除子视图中的所有内容,但是我我不确定这可能是什么。我希望我的错误对于那些有更多经验的人来说显而易见,并且可以为我指明正确的方向。

enter image description here

我在下面也分享了一些代码,但是我怀疑视觉效果可能是经验丰富的人最直接的线索。

var topOfViewFrame: CGFloat = 0
textViewArray = []
for textBlock in textBlocks.textBlocksArray { // a textBlock has formatting data for a UITextView
    let textBlockHeight = CGFloat(textBlock.numberOfLines) * textBlock.blockFontSize
    let viewFrame = CGRect(x: 0, y: topOfViewFrame, width: textBoxWidth, height: textBlockHeight)
    var newTextView = UITextView(frame: viewFrame)
    newTextView.center = CGPoint(x: screenView.frame.width/2, y: topOfViewFrame + (textBlockHeight/2)) // screenView is the 320x240 subView holding the [UITextViews]
    let viewFont = UIFont(name: "AvenirNextCondensed-Medium", size: textBlock.blockFontSize)
    newTextView.font = viewFont
    newTextView.text = textBlock.blockText
    newTextView = configureTextBlockView(textBoxView: newTextView, textBlock: textBlock)
    textViewArray.append(newTextView)
    screenView.addSubview(newTextView) // screenView is the subview holding the [UITextViews]
    topOfViewFrame += newTextView.frame.height // put the next UITextView directly below the previous one
}

谢谢!

1 个答案:

答案 0 :(得分:2)

如果要从“ screenView”中删除所有textViews,仅清除数组是不够的。 您需要将其从超级视图中删除:

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