使用消息更新 swift iOS 上的 TableViewCell

Updating the TableViewCell on swift iOS with a message

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

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, for: .normal)
    button.layer.cornerRadius = 12
    button.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
    button.layer.masksToBounds = true
    button.addTarget( self, 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
    
}
}

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

我尝试了一些点击发送按钮的方法。在 table 视图 cellForRowAt 我也尝试过。

我很抱歉问了一个对大多数程序员来说可能很简单的问题。我是新人。

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

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

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

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

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

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

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

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

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