【问题标题】:Updating the TableViewCell on swift iOS with a message使用消息更新 swift iOS 上的 TableViewCell
【发布时间】:2021-02-13 01:40:54
【问题描述】:

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

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
    
}
}

当我点击发送按钮时,我希望我的单元格更新并显示我发送的消息。 我不知道在我的发送按钮 func 中放了什么,或者我是否以不同的方式来解决这个问题。

我在点击发送按钮的情况下尝试了几次。在 table view cellForRowAt 我也试过了。

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

【问题讨论】:

    标签: ios swift uitableview viewcontroller


    【解决方案1】:

    首先,您不应将文本字段添加到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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多