【发布时间】:2020-07-10 08:27:48
【问题描述】:
我真的不明白为什么会这样。如果文本已更改,我将更改 UITableView 内的 UITextField 的文本颜色,但是当我向下或向上滚动 tableView 时,其他 UITextFields 会更改文本颜色。 如何解决这个问题?
这是我的代码和问题的一些截图:
import Foundation
import UIKit
private let reuseIdentifier = "ApcGenericCell"
private let portIdentifier = "ApcPortCell"
private let addressIdentifier = "ApcAddressCell"
class MyViewController: UIViewController {
private var tableView: UITableView!
private var bottomConstraint: NSLayoutConstraint!
private var newBottomConstraint: NSLayoutConstraint!
var apc = [Apc]()
override func viewDidLoad() {
super.viewDidLoad()
// configure the table view for the left menu
configureTableView()
}
private func configureTableView() {
tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = 60
tableView.register(UINib(nibName: "ApcGeneric", bundle: nil), forCellReuseIdentifier: reuseIdentifier)
tableView.register(UINib(nibName: "ApcPortAt", bundle: nil), forCellReuseIdentifier: portIdentifier)
tableView.register(UINib(nibName: "ApcIpAddress", bundle: nil), forCellReuseIdentifier: addressIdentifier)
self.view.addSubview(tableView)
tableView.backgroundColor = .clear
tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: view.frame.height / 8).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: view.frame.width / 8).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -(view.frame.width / 8)).isActive = true
bottomConstraint = tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -(view.frame.height / 8))
bottomConstraint.isActive = true
tableView.alwaysBounceVertical = false
tableView.tableFooterView = UIView(frame: .zero)
// register for notifications when the keyboard appears and disappears:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(note:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(note:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(receivedMessage), name: Notification.Name("ReceivedMessage"), object: nil)
apc = ApcSection.emptySection()
self.tableView.reloadData()
}
@objc func receivedMessage() {
DispatchQueue.global(qos: .default).async {
ApcSection.fetchData(map: self.apcConfig, { (apcResult) in
self.apc = apcResult
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
}
// Handle keyboard frame changes here.
// Use the CGRect stored in the notification to determine what part of the screen the keyboard will cover.
// Adjust our table view's bottomAnchor so that the table view content avoids the part of the screen covered by the keyboard
@objc func keyboardWillShow(note: NSNotification) {
// read the CGRect from the notification (if any)
if let newFrame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if bottomConstraint.isActive {
bottomConstraint.isActive = false
newBottomConstraint = tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -newFrame.height)
newBottomConstraint.isActive = true
tableView.updateConstraints()
}
}
}
// User dismiss the keyboard
@objc func keyboardWillHide(note: NSNotification) {
newBottomConstraint.isActive = false
bottomConstraint.isActive = true
tableView.updateConstraints()
}
@objc func textHasChanged(sender: UITextField) {
let cell = sender.superview?.superview as! UITableViewCell
let indexPath = tableView.indexPath(for: cell)
if let index = indexPath?.row {
if let _ = apc[index] {
// change textColor if the value has been changed
if sender.text != apc[index]!) {
sender.textColor = .systemRed
} else {
sender.textColor = .myBlue
}
}
}
}
}
extension MyViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return apc.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: portIdentifier, for: indexPath) as! ApcPortAt
cell.selectionStyle = .none
return cell
} else if indexPath.row == 7 || indexPath.row == 9 {
let cell = tableView.dequeueReusableCell(withIdentifier: addressIdentifier, for: indexPath) as! ApcIpAddress
cell.selectionStyle = .none
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! ApcGeneric
cell.value.text = apc[indexPath.row]
cell.value.delegate = self
cell.value.addTarget(self, action: #selector(textHasChanged(sender:)), for: .editingChanged)
cell.selectionStyle = .none
return cell
}
}
}
【问题讨论】:
-
我认为这里要记住的关键是,你的单元格不是
static,如果UITableViewCell一遍又一遍,UITableView将重复使用同一个实例,这就是为什么你有dequeueReusableCell。UITableView决定何时需要创建新单元或重用已缓存的单元。所以,要记住的是,当你出列一个单元格时,你需要重置它的状态以满足它所代表的数据的要求 -
感谢 MadProgrammer 的解释。
标签: ios swift uitableview uitextfield swift5