如何解决使用 UITextField 重用单元格的问题
问题 1) 是:UITextFields 在提示后以随意的方式显示/隐藏文本。例如,您在名称字段中输入“john”,向上/向下滚动,您将在其他字段中看到“john”
问题 2) 滚动数据时提示消失,因此填充字段是有意义的。
细胞协议
protocol TableViewCellProtocol /*UpperDelegate*/ {
var identity: String { get set }
var cellClass: UITableViewCell.Type { get set }
func config(cell: UITableViewCell)
}
extension UIView {
// static var identifier: String { //usa questa nel mondo reale
static var identifier_GivenByExtension: String {
return String(describing: self)
}
}
我的手机
protocol ValidationSimpleCell_Delegate: class {
func getDataFromCell(fromCell: String,data: String)
func canEnableButton(fromField: String,statusForEnableIs: Bool)
}
import UIKit
class ValidationSimpleCell: UITableViewCell {
@IBOutlet weak var cellTextfield: UITextField!
weak var delegate: ValidationSimpleCell_Delegate?
private var model: ValidationSimpleCell_RowObject?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool,animated: Bool) {
super.setSelected(selected,animated: animated)
}
func configCell(_ model: ValidationSimpleCell_RowObject) {
self.model = model
delegate = model.delegate
styleUI(model)
cellTextfield.delegate = self
}
private func styleUI(_ model: ValidationSimpleCell_RowObject ) {
self.backgroundColor = model.background
self.cellTextfield.clearButtonMode = .whileEditing
self.cellTextfield.placeholder = model.identity
switch model.identity {
case ProfileSections.firstSection.rawValue,ProfileSections.secondSection.rawValue:
self.cellTextfield.isHidden = true
default:
self.cellTextfield.isHidden = false
}
}
}
//MARK: - object for tableView
class ValidationSimpleCell_RowObject: TableViewCellProtocol {
var identity: String
var cellClass: UITableViewCell.Type = ValidationSimpleCell.self
var delegate: ValidationSimpleCell_Delegate?
var background: UIColor
init(
delegate: ValidationSimpleCell_Delegate? = nil,identity: String,background: UIColor = .systemBackground
) {
self.delegate = delegate
self.identity = identity
self.background = background
}
func config(cell: UITableViewCell) {
(cell as? ValidationSimpleCell)?.configCell(self)
}
}
extension ValidationSimpleCell: UITextFieldDelegate {
//per la validazione sembra più affidabile
func textFieldDidChangeSelection(_ textField: UITextField) {
cell_CheckFieldsEmptiness(checkValidity: true)
delegate?.getDataFromCell(fromCell: model?.identity ?? "N/A",data: textField.text ?? "N/aoA")
// SC_cell_CheckFieldsEmptiness(cellTextfield: cellTextfield,model: self.model)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == cellTextfield {
let _ = cellTextfield.resignFirstResponder()
}
return true
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
cell_CheckFieldsEmptiness(checkValidity: true)
// SC_cell_CheckFieldsEmptiness(cellTextfield: cellTextfield,model: self.model)
return true
}
}
protocol CellCanCheckDelegate {
func cell_CheckFieldsEmptiness(checkValidity: Bool) //putt default = true
}
extension ValidationSimpleCell : CellCanCheckDelegate {
func cell_CheckFieldsEmptiness(checkValidity: Bool = true) {
let textFromTextfield = cellTextfield.text ?? ""
var isLongEough = true
var isMailValid = true
if checkValidity {
let isFieldEmpty = textFromTextfield.isEmpty
if self.model?.identity == Fields.name.rawValue {
}
switch self.model?.identity {
case Fields.name.rawValue:
// isLongEough = isMinimumLong(text: testo,limit: 4)
isLongEough = textFromTextfield.isMinimumLong(limit: 4)
isLongEough = !textFromTextfield.isNumber()
case Fields.mailAddress.rawValue:
isMailValid = textFromTextfield.isValidEmail()
default:
break
}
if !isFieldEmpty && isLongEough && isMailValid {
self.model?.delegate?.canEnableButton(fromField: self.model?.identity ?? "N/A",statusForEnableIs: true)
} else {
self.model?.delegate?.canEnableButton(fromField: self.model?.identity ?? "N/A",statusForEnableIs: false)
}
}
}
}
视图控制器
enum ProfileSections: String,CaseIterable {
case firstSection = "First Section"
case secondSection = "Second Section"
}
enum Fields: String,CaseIterable {
case name = "Type Your Name"
case surname = "type Your Surname"
case mailAddress = "type Your Email"
case dato1
case dato2
case dato3
case dato4
case dato5
case dato6
case dato7
case dato8
case dato9
case dato10
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myTableView: UITableView!
var rows: [TableViewCellProtocol] = []
//usati per validazione
@IBOutlet weak var footerButtonOut: UIButton!
private var TEMP_Name = false
private var theName = ""
private var TEMP_surname = false
private var theSurname = ""
private var TEMP_mail = false
private var theMail = ""
private var goingForwards = false
override func viewDidLoad() {
super.viewDidLoad()
setUpTableView()
fillRowsWithObjects()
self.footerButtonOut.isEnabled = false
}
private func setUpTableView() {
myTableView.delegate = self
myTableView.dataSource = self
myTableView.tableFooterView = UIView() //hides unwanted rows
}
private func fillRowsWithObjects() {
self.rows = []
for section in ProfileSections.allCases {
self.rows.append(ValidationSimpleCell_RowObject(delegate: nil,identity: section.rawValue,background: .lightGray))
switch section {
case .firstSection:
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.name.rawValue))
case .secondSection:
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.surname.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.mailAddress.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato1.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato2.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato3.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato4.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato5.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato6.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato7.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato8.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato9.rawValue))
self.rows.append(ValidationSimpleCell_RowObject(delegate: self,identity: Fields.dato10.rawValue))
break
}
}
self.myTableView.reloadData()
}
@IBAction func footerButtonTapped(_ sender: Any) {
print("tapped")
}
}
//MARK: - tableView section
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
let selected = self.rows[indexPath.row]
// switch selected.identity {
// case Fields.name.rawValue:
// print("go to name")
// case Fields.surname.rawValue:
// print("go to surname")
// case Fields.mailAddress.rawValue:
// print("got o mail")
// default:
// break
// }
self.myTableView.deselectRow(at: indexPath,animated: true)
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return self.rows.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = rows[indexPath.row]
let cell = self.myTableView.dequeueReusableCell(withIdentifier: model.cellClass.identifier_GivenByExtension,for: indexPath)
model.config(cell: cell)
return cell
}
}
extension ViewController: ValidationSimpleCell_Delegate {
func canEnableButton(fromField: String,statusForEnableIs: Bool) {
switch fromField {
case Fields.name.rawValue:
self.TEMP_Name = statusForEnableIs
case Fields.surname.rawValue:
self.TEMP_surname = statusForEnableIs
case Fields.mailAddress.rawValue:
self.TEMP_mail = statusForEnableIs
default:
break
}
let requiredFields = TEMP_Name && TEMP_surname && TEMP_mail
if requiredFields {
self.footerButtonOut.isEnabled = true
} else {
self.footerButtonOut.isEnabled = false
}
}
func getDataFromCell(fromCell: String,data: String) {
switch fromCell {
case Fields.name.rawValue:
self.theName = data
print("SAVE ON USEDEF \(data)")
case Fields.surname.rawValue:
self.theSurname = data
print("SAVE ON USEDEF \(data)")
case Fields.mailAddress.rawValue:
print("SAVE ON USEDEF \(data)")
default:
break
}
}
}
解决方法
- 您需要阅读有关可重用单元格的内容;)
- 将值存储在控制器中并在 cellForRowAt 的单元格中显示它们...
- 当您更改单元格中的某些值时,您还应该将它们存储(闭包,委托...)在控制器中
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。