This is the code that I have for Table View's cell class RegisterCell class (Table view cell) and this is the code in my controller part 1 (view did load, view did appear functions, cellForRowAt function and error. My goal is to make the horizontalview in registerCell's class change the background color every-time the corresponding textfield is clicked (or typed in). Whats the issue for this error? Any help is highly appreciated. This is what I mean by corresponding texfield and view the controller generates 6 of those
Code shown below for registerCell class
import Foundation
import UIKit
class RegisterCell: UITableViewCell, UITextFieldDelegate{
@IBOutlet weak var userInfoLbl: UILabel!
@IBOutlet weak var userInfoTxtField: UITextField!
@IBOutlet weak var horizontalView: UIView!
func textFieldDidBeginEditing(_ textField: UITextField){
horizontalView.backgroundColor = UIColor(red: 0.0, green: 175.0/210.0, blue: 212.0/188.0, alpha: 1)
}
}
Code shown below for RegisterViewController:
override func viewDidLoad() {
super.viewDidLoad()
editButtonColorAndShapes()
configure()
self.tableView.separatorColor = .clear;
self.tableView.delegate = self
self.tableView.dataSource = self
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
configure()
self.tableView.register(RegisterCell.self, forCellReuseIdentifier: "registerCell")
editButtonColorAndShapes()
//r
}
@objc func textFieldDidChange(textfield: UITextField) {
switch textfield.tag {
case 0:
firstname = textfield.text!
case 1:
lastname = textfield.text!
case 2:
pnumber = textfield.text!
case 3:
email = textfield.text!
case 4:
passwrd = textfield.text!
textfield.isSecureTextEntry = true
case 5:
zipCode = textfield.text!
default:
break
}
registerModell = RegisterModel(fname: firstname, lname: lastname, fhone: pnumber, mail: email, pass: passwrd, zip: zipCode)
}
extension RegisterViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "registerCell",
for: indexPath) as! RegisterCell
cell.selectionStyle = .none
cell.userInfoLbl?.text = titles[indexPath.row]
cell.userInfoLbl?.textColor = UIColor(named: "aquamarine")
cell.horizontalView.backgroundColor = UIColor(red: 0.0, green: 175.0/210.0, blue: 212.0/188.0, alpha: 0.25)
cell.userInfoLbl?.addCharacterSpacing(kernValue: 4.57)
cell.userInfoTxtField.delegate = delegatez
cell.userInfoTxtField.tag = indexPath.row
cell.userInfoTxtField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
cell.configure()
return cell
}
CodePudding user response:
My only guess to why your horizontalView
had an error is due to the IBOutlets
not being connected properly somehow (maybe delete the outlet and then replace it again) However, I have gotten the horizontalView
to change its own background colour when the user taps inside of the textField
to begin typing/editing.
The reason for why even without the error you were getting, the horizontal view was not changing color was due to the fact that you had not set the Delegate
of the text field to its cell class.
NOTE: The delegate listens out for and handles events such as touching and typing therefore it is required for your desired outcome.
This is how I did it:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! RegisterCell
cell.userInfoTxtField.delegate = cell
return cell
}
CodePudding user response:
Assuming that you place textField
in a contentView of RegisterCell
, you could refer to a cell as a superview of a superview for the textField. After that you could change background color for horizontalView
:
func textFieldDidBeginEditing(_ textField: UITextField) {
if let cell = textField.superview?.superview as? RegisterCell {
cell.horizontalView.backgroundColor = UIColor(red: 0.0, green: 175.0/210.0, blue: 212.0/188.0, alpha: 1)
}
}