繁体   English   中英

即使我清除了数组,iOS UITextViews数据仍然存在

[英]iOS UITextViews data lingering even though I clear out an array

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

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

在此处输入图片说明

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

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
}

谢谢!

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

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

暂无
暂无

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

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