我有一个tableView,由于某种原因,单元格在重复自己。我已经尝试解决了一段时间,但是我却无法做到。
我的代码为
//自定义单元格
import UIKit
class SettingsCell: UITableViewCell {
class SettingsTextField: UITextField {
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 24, dy: 0)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 24, dy: 0)
}
override var intrinsicContentSize: CGSize {
return .init(width: 0, height: 44)
}
}
let pntextField: UITextField = {
let mycolor = GREEN_Theme
let tf = SettingsTextField()
// tf.keyboardType = .numberPad
return tf
}()
let textField: UITextField = {
let mycolor = GREEN_Theme
let tf = SettingsTextField()
tf.layer.cornerRadius = 10
tf.layer.borderWidth = 1.0
tf.textAlignment = .left
// tf.contentVerticalAlignment = .top
tf.layer.borderColor = mycolor.cgColor
return tf
}()
let textField2: UITextField = {
let mycolor = GREEN_Theme
let tf = SettingsTextField()
tf.layer.cornerRadius = 10
tf.layer.borderWidth = 1.0
tf.textAlignment = .left
// tf.contentVerticalAlignment = .top
tf.layer.borderColor = mycolor.cgColor
return tf
}()
let textField3: UITextField = {
let mycolor = GREEN_Theme
let tf = SettingsTextField()
tf.layer.cornerRadius = 10
tf.layer.borderWidth = 1.0
tf.textAlignment = .left
// tf.contentVerticalAlignment = .top
tf.layer.borderColor = mycolor.cgColor
return tf
}()
let textField4: UITextField = {
let mycolor = GREEN_Theme
let tf = SettingsTextField()
tf.layer.cornerRadius = 10
tf.layer.borderWidth = 1.0
tf.textAlignment = .left
// tf.contentVerticalAlignment = .top
tf.layer.borderColor = mycolor.cgColor
return tf
}()
let textField5: UITextField = {
let mycolor = GREEN_Theme
let tf = SettingsTextField()
tf.layer.cornerRadius = 10
tf.translatesAutoresizingMaskIntoConstraints = false
tf.sizeToFit()
//tf.scrollEnabled = false
tf.layer.borderWidth = 1.0
tf.textAlignment = .left
tf.layer.borderColor = mycolor.cgColor
return tf
}()
let Phone: UILabel = {
let label = Label()
label.text = "Phone"
return label
}()
class Label: UILabel {
override var intrinsicContentSize: CGSize {
return .init(width: 80, height: 0)
}
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let texttackView = UIStackView(arrangedSubviews: [
UIStackView(arrangedSubviews: [pntextField]),
UIStackView(arrangedSubviews: [textField]),
UIStackView(arrangedSubviews: [textField2])])
texttackView.axis = .vertical
texttackView.spacing = 1
addSubview(texttackView)
texttackView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 16, left: 16, bottom: 16, right: 16))
textField.fillSuperview()
let textstackView = UIStackView(arrangedSubviews: [
UIStackView(arrangedSubviews: [textField3]),
UIStackView(arrangedSubviews: [textField4]),
UIStackView(arrangedSubviews: [textField5])])
textstackView.axis = .vertical
textstackView.spacing = 1
addSubview(textstackView)
textstackView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 16, left: 16, bottom: 16, right: 16))
textField.fillSuperview()
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
}
// tableView
override func numberOfSections(in tableView: UITableView) -> Int {
return 9
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? 0 : 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 1 {
let mileRangeCell = MileRangeCell(style: .default, reuseIdentifier: nil)
mileRangeCell.mileSlider.addTarget(self, action: #selector(handleMileChange), for: .valueChanged)
let mile = user?.mileRange ?? SettingsViewController.defaultMile
mileRangeCell.mileLabel.text = "Miles \(mile)"
mileRangeCell.mileSlider.value = Float(mile)
return mileRangeCell
}
if indexPath.section == 2 {
let costRangeCell = AgeRangeCell(style: .default, reuseIdentifier: nil)
costRangeCell.minSlider.addTarget(self, action: #selector(handleMinAgeChange), for: .valueChanged)
costRangeCell.maxSlider.addTarget(self, action: #selector(handleMaxAgeChange), for: .valueChanged)
let minAge = user?.minSeekingCost ?? SettingsViewController.defaultMinSeekingCost
let maxAge = user?.maxSeekingCost ?? SettingsViewController.defaultMaxSeekingCost
costRangeCell.minLabel.text = "Min $\(minAge)"
costRangeCell.maxLabel.text = "Max $\(maxAge)"
costRangeCell.minSlider.value = Float(minAge)
costRangeCell.maxSlider.value = Float(maxAge)
return costRangeCell
}
let cell = SettingsCell(style: .default, reuseIdentifier: nil)
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else { return }
let user = User(dictionary: dictionary as [String : AnyObject])
switch indexPath.section {
case 3:
cell.pntextField.placeholder = "Enter Phone"
cell.pntextField.text = user.number
cell.pntextField.addTarget(self, action: #selector(self.handlePhoneChange), for: .editingChanged)
cell.textField.placeholder = "Enter Email"
cell.textField.text = user.email
cell.textField.addTarget(self, action: #selector(self.handleEmailChange), for: .editingChanged)
cell.textField2.placeholder = "Enter Address"
cell.textField2.text = user.address
cell.textField2.addTarget(self, action: #selector(self.handleAddressChange), for: .editingChanged)
case 4:
cell.textField3.placeholder = "Enter Skills"
cell.textField3.text = user.skills
cell.textField3.addTarget(self, action: #selector(self.handleSkillsChange), for: .editingChanged)
cell.textField4.placeholder = "Enter Education"
cell.textField4.text = user.education
cell.textField4.addTarget(self, action: #selector(self.handleEducationChange), for: .editingChanged)
cell.textField5.placeholder = "Enter Bio"
cell.textField5.text = user.bio
cell.textField5.addTarget(self, action: #selector(self.handleBioChange), for: .editingChanged)
}
}, withCancel: { (err) in
print("attempting to load information")
})
return cell
}
在我的视图控制器中,我看到image repeating
我只希望textViews在情况3和4中出现。我希望自定义单元格结束,而不要在其余单元格中重复。如果有人可以引导我到正确的地方,我将非常感激。
我为大量代码表示歉意,我认为这与自定义单元格中的useuseIdentifier有关,但我不太确定。