【发布时间】: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