使用消息在 swift iOS 上更新 TableViewCell

问题描述

这就是我的视图控制器中的内容。非常简单,有一个发送按钮和一个带有约束和表格视图的消息字段。

import UIKit

class ViewController: UIViewController {

public let identifier = "ViewController"

private let tableView: UITableView = {
    let table = UITableView()
    table.register(SecureTextViewCell.self,forCellReuseIdentifier: SecureTextViewCell.identifier)
    return table
    
}()


private let secureButton : UIButton = {
    let button = UIButton()
    button.setTitle("Secure",for: .normal)
    button.backgroundColor = .link
    button.setTitleColor(.white,for: .normal)
    button.layer.cornerRadius = 12
    button.titleLabel?.font = .systemFont(ofSize: 20,weight: .bold)
    button.layer.masksToBounds = true
    button.addTarget( self,action: #selector(securebuttonTapped),for: .touchUpInside)
    return button
    
}()


let sendButton : UIButton = {
    let button = UIButton()
    button.setTitle("Send",for: .normal)
    button.backgroundColor = .green
    button.setTitleColor(.white,action: #selector(sendButtonTapped),for: .touchUpInside)
    return button
}()

let textView : UITextField = {
    let text = UITextField()
    text.isHidden = true
    text.isSecureTextEntry = true
    text.backgroundColor = .red
    text.isUserInteractionEnabled = false
    return text
}()

private let messageField: UITextField = {
    let field = UITextField()
    field.autocapitalizationType = .none
    field.autocorrectionType = .no
    field.returnKeyType = .done
    field.layer.cornerRadius = 12
    field.layer.borderWidth = 1
    field.layer.borderColor = UIColor.lightGray.cgColor
    field.placeholder = "Secure Text Message"
    field.leftViewMode = .always
    field.backgroundColor = .white
    field.isOpaque = true
    field.clearButtonMode = .always
    return field
}()
var passwordFieldConstraints : NSLayoutConstraint!
var sendButtonFieldCoonstraints: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate = self
    tableView.dataSource = self
    view.addSubview(tableView)
    view.addSubview(messageField)
    view.addSubview(sendButton)
    
    messageField.translatesAutoresizingMaskIntoConstraints = false
    sendButton.translatesAutoresizingMaskIntoConstraints = false
    messageField.addTarget(self,action: #selector(buttonAction),for: .touchUpInside)
    sendButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
    sendButton.widthAnchor.constraint(equalTo: tableView.widthAnchor).isActive = true
    

    messageField.heightAnchor.constraint(equalToConstant: 40).isActive = true
    messageField.widthAnchor.constraint(equalTo: tableView.widthAnchor).isActive = true
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    tableView.frame = view.bounds
    textView.frame = CGRect(x: 0,y: 300,width: view.width,height: 30)
}

@objc func sendButtonTapped() {
    
    print("send button tapped")
}


}


extension ViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    
    return 5
 }
 func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) ->           UITableViewCell {
    let text = messageField.text!
    let cell = tableView.dequeueReusableCell(withIdentifier: TextViewCell.identifier,for: indexPath) as! TextViewCell
    cell.configure(with: text)
    return cell
    
}
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80
}
}

这是我的 tableViewCell,它应该使用我在消息字段中的文本进行更新吗?

import Foundation
import UIKit

class TextViewCell: UITableViewCell {

static let identifier = "SecureTextViewCell"


let secureText : UITextField = {
    let secureText = UITextField()
    secureText.text = ""
    secureText.isUserInteractionEnabled = false
    secureText.backgroundColor = .link
    return secureText
}()

override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
    super .init(style: style,reuseIdentifier: reuseIdentifier)
    
    contentView.addSubview(secureText)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    super.layoutSubviews()
    contentView.addSubview(secureText)
    secureText.frame = CGRect(x: 0,y: 0,width: contentView.width,height: contentView.height)
}

public func configure(with model: String) {
    self.secureText.text = model
    
}
}

当我点击发送按钮时,我希望我的手机更新并显示我发送的消息。 我不知道在我的发送按钮函数中放了什么,或者我是否以不同的方式处理这个。

我在点击发送按钮的情况下尝试了一些。在表格视图 cellForRowAt 上我也尝试过。

我很抱歉问了对大多数编码人员来说可能是一件简单的事情。我是新来的。

解决方法

首先,您不应将文本字段添加到 layoutSubviews 中的单元格子视图,因为每次需要更新单元格布局时都会调用它。根据 documentation,您应该只放置代码来更新无法自动更新的子视图的布局:

子类可以根据需要重写此方法以执行更精确的 其子视图的布局。仅当 子视图的自动调整大小和基于约束的行为不会 提供您想要的行为。您可以使用您的实现来设置 您的子视图的框架矩形直接。

因此将单元格中的 layoutSubviews 函数更新为:

override func layoutSubviews() {
   super.layoutSubviews()
   secureText.frame = contentView.bounds
}

现在从视图控制器更改单元格的文本可能就像这样简单:

@objc func sendButtonTapped() {
    tableView.reloadData()
}

这将重新加载表格的所有可见单元格,因为您已经在 tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell 中设置了文本配置,所以每个单元格都将显示 messageField 的文本。

如果您只想更新一个特定的单元格,您将需要更改单元格配置逻辑,然后为准确的单元格调用 reload:

@objc func sendButtonTapped() {
    let indexPath = IndexPath(row: 0,section: 0) //index of cell to reload
    tableView.reloadRows(at: [indexPath],with: .automatic)
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...