【问题标题】:Text moving between table view cells在表格视图单元格之间移动的文本
【发布时间】:2020-02-22 17:11:16
【问题描述】:

我有一个表格视图,其中包含多个部分,每个部分的行数不同。

我创建了一个自定义表格视图单元格。该单元格有一个标签和一个文本字段。

相同的自定义单元格用于表格视图中的每个单元格。

当我在文本字段中输入值时,它会出现在其他一些文本字段以及其他行中。有时文本会从我输入的文本字段中消失,并出现在下面的行中。当我上下滚动 tableview 时会发生这种情况。

这是我用于 cellForRowAt 的代码

let cell = tableView.dequeueReusableCell(withIdentifier:   "LabelTextFieldTableViewCell", for: indexPath) as! LabelTextFieldTableViewCell
    for i in approachSection {
        if Int(i.row) == indexPath.row {
            cell.myLabel.text = i.name
            cell.myTextField.placeholder = cell.setPlaceholder(type: i.type!)
            if i.type != CustomFieldType.text.rawValue {
                cell.isInteger = true
            }
        }
    }
return cell

自定义单元格是

class LabelTextFieldTableViewCell: UITableViewCell, UITextFieldDelegate {

    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var myTextField: UITextField!

    var isInteger: Bool?
    var type: String!

    override func awakeFromNib() {
        super.awakeFromNib()
        myTextField.delegate = self
        myTextField.textAlignment = .right
        myTextField.borderStyle = .none


    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setPlaceholder(type: String) -> String {
        switch type {
        case CustomFieldType.count.rawValue:
            return "#"
        case CustomFieldType.time.rawValue:
            return "Time as 1234"
        case CustomFieldType.text.rawValue:
            return "Enter Text"
        default:
            return ""
        }
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        textField.textColor = .black
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        guard textField.text != "" else { return }

        if isInteger != nil {
            guard checkIfIntegers(str: textField.text!) != false else {
                print("Not all Integers")
                textField.textColor = .red
                return
            }
        }
    }

}

【问题讨论】:

  • bcoz of dequeueReusableCell,在 UITablview 中,您的单元格正在被重用,因此当您在任何一个文本字段中输入文本时,它将出现在不同索引的单元格中。另外我建议你先学习UITablview的基础知识,有很多教程。
  • 我认为您应该跟踪文本字段已更改的单元格。每次调用 cellForRow 时,都要确保文本字段已初始化。

标签: ios swift uitableview tableview


【解决方案1】:

在您的单元子类中使用 prepareToReuse 方法。在这里您应该将所有值重置为默认值,并在相应的 cellForRowAtIndexPath 方法中将适当的值设置为单元格。

func prepareForReuse()

Apple documentation

在你的LabelTextFieldTableViewCell 类中编写这个方法 我希望它会帮助你 快乐编码 =)

【讨论】:

    【解决方案2】:

    请确保您正确遵循此算法

    1. 首先,您将决定需要多少个部分?
    2. 然后您将决定一个部分中需要多少行
    3. 然后您将从单元格中为您的数据源模型分配每一行,因为您不必使用 for 循环。 Index.row 将为您完成这项工作。

    注意:您可以在这些方法中使用 switch 语句来决定在哪个特定部分显示多少行。

    - tableView:cellForRowAtIndexPath: // required method
    
    – tableView:numberOfRowsInSection: // required method
    
    – numberOfSectionsInTableView:
    
    – sectionIndexTitlesForTableView:
    
    – tableView:sectionForSectionIndexTitle:atIndex:
    
    – tableView:titleForHeaderInSection:
    
    – tableView:titleForFooterInSection:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多