微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

迅速更新 UITableViewCell 中的 UITextView 时出错

如何解决迅速更新 UITableViewCell 中的 UITextView 时出错

我有一个 UITableView,其中注册了多个单元格。

private lazy var table: UITableView = {
    let table = UITableView(frame: .zero,style: .plain)
    table.backgroundColor = .clear
    table.separatorStyle = .none

    table.dataSource = self.dataProvider
    table.delegate = self.dataProvider

    table.rowHeight = UITableView.automaticDimension
    table.autoresizesSubviews = true
    table.estimatedRowHeight = CreateContentLayout.cellHeight

    table.register(CreateContentTextCell.classForCoder(),forCellReuseIdentifier: CreateContentTextCell.self.nameOfClass())
    table.register(CCOutlinedTextAreaCell.classForCoder(),forCellReuseIdentifier: CCOutlinedTextAreaCell.self.nameOfClass())
    table.register(CreateContentButtonCell.classForCoder(),forCellReuseIdentifier: CreateContentButtonCell.self.nameOfClass())
    table.register(CreateContentPhotoCell.classForCoder(),forCellReuseIdentifier: CreateContentPhotoCell.self.nameOfClass())
    table.register(CreateContentLableCell.classForCoder(),forCellReuseIdentifier: CreateContentLableCell.self.nameOfClass())
    table.register(CreatContetnAttachmentsCell.classForCoder(),forCellReuseIdentifier: CreatContetnAttachmentsCell.self.nameOfClass())

    table.contentInset = .zero
    table.showsverticalScrollIndicator = false
    table.keyboarddismissMode = .onDrag
    
    table.tableFooterView = UIView(frame: .zero)

    return table
}()

一个单元格用于输入文本,另一个用于附加图像。

用于输入文本的单元格包含一个 UITextView。输入文本时,UITextView 和 UITableViewCell 会增加高度,一切正常。

private lazy var textField: MDCOutlinedTextArea = {
    let field = MDCOutlinedTextArea()
    let title: String
    
    field.minimumNumberOfVisibleRows = 1
    field.maximumNumberOfVisibleRows = .infinity
    field.textView.font = CreateContentLayout.lableAboutFont
    field.setoutlineColor(Color.gray,for: .normal)
    field.setoutlineColor(Color.blueGray,for: .editing)
    field.setTextColor(.black,for: .normal)
    field.setTextColor(.black,for: .editing)
    field.setFloatingLabel(Color.blueGray,for: .normal)
    field.setFloatingLabel(Color.blueGray,for: .editing)

    switch self.data {
    case let .subject(_,value,error),let .text(_,let .phone(value,error)/*,let .region(value,error)*/,let .recipeInfo(value,let .ingredients(value,let .email(value,let .communications(value,error):
        field.textView.backgroundColor = (value.isEmpty ? .clear : .white)
        field.textView.text = value
    default:
        break
    }

    
    field.textView.delegate = self
    
    return field
}()

func textViewDidChange(_ textView: UITextView) {
    let size = textView.bounds.size
    let newSize = textView.sizeThatFits(CGSize(width: size.width,height: CGFloat.greatestFiniteMagnitude))
    let newHeight = (textField.sizeThatFits(CGSize(width: size.width,height: CGFloat.greatestFiniteMagnitude))).height

    if size.height != newSize.height {
        UIView.setAnimationsEnabled(false)
        tableView?.beginUpdates()
        
        self.textField.snp.updateConstraints {
            $0.height.equalTo(newHeight)
        }
        tableView?.endUpdates()
        UIView.setAnimationsEnabled(true)
    }

    self.scrollToRect(textView)
}

func setupUI() {
self.contentView.addSubview(textField)
        
        self.textField.snp.makeConstraints {
            $0.left.equalTo(BaseLayout.viewHorizontalPadding)
            $0.right.equalTo(BaseLayout.viewHorizontalPadding.negated)
            $0.top.equalTo(BaseLayout.viewHorizontalPadding)
            $0.bottom.equalTo(BaseLayout.viewHorizontalPadding.negated).priority(.low)
        }
        
        NotificationCenter.default.addobserver(
            self,selector: #selector(textDidChange(_:)),name: UITextView.textDidChangeNotification,object: self.textField.textView
        )
        
        self.layoutIfNeeded()
                                
        let newSize = self.textField.sizeThatFits(CGSize(
            width: UIScreen.main.bounds.width - BaseLayout.viewHorizontalPadding * 2,height: CGFloat.greatestFiniteMagnitude
        ))
        self.textField.snp.makeConstraints {
            $0.height.equalTo(newSize.height)
        }

但是,当我添加图像并且 UITableView 刷新单元格时,下面会出现一个额外的区域。

更多视频

error video

debug video

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。