简体   繁体   中英

iOS UITextViews data lingering even though I clear out an array

The image below shows Xcode graphical debug hierarchy for a UIViewController . It looks like I need to destroy additional data from UITextViews that are being recreated after editing in an array of UITextViews . Each time I make a change, I've set the array of UITextViews to [], then recreate it with the updated data. Even though I verify that the UITextView array is indeed being reset to zero elements, then recreated with the expected number of elements, these ghost images linger on screen and the debug hierarchy shows something isn't being removed.

I suspect there's some sort of housekeeping I need to do to find & destroy additional data related to the UITextViews , and that setting the array back to zero isn't clearing everything out of my subview, but I'm unsure what this might be. I'm hopeful that my mistake seems obvious to those with more experience & you'll point me in the right direction.

在此处输入图片说明

I also share some code below, but I suspect the visual may be the most direct clue for the experienced to set me straight.

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
}

Thanks!

If you want to remove all textViews from 'screenView' its not enough just to clear the array. You need to remove it from superview:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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