Переработанные / повторяющиеся ячейки в табличном представлении - PullRequest
0 голосов
/ 08 июля 2019

У меня есть 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
}

в моем контроллере вида я вижу повторение изображения

повторение # 2

Я хочу, чтобы textViews был только в случаях 3 и 4. и я хотел бы, чтобы пользовательская ячейка заканчивалась,не повторяться в остальных клетках.Я был бы очень признателен, если бы кто-то мог направить меня в нужное место.

Я прошу прощения за большой объем кода, я думаю, что это связано с reuseIdentifier в пользовательской ячейке, но я не слишком уверен.

...