首页 > 解决方案 > 在 Swift 的 UITextfield 中滚动 Tableview 数据重新洗牌

问题描述

我有表格视图,其中单元格由多个部分组成,每个部分有不同的行数,每行都有一个文本字段。当我在其中写入并向下和向上滚动时,数据丢失或重新排列。所以我试图将文本字段数据保存到二维数组中,但我无法解决这个问题。

这是自定义单元格中的代码:

class CustomCell: UITableViewCell {

    @IBOutlet weak var userName: UITextField!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}

查看控制器代码:

extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label = UILabel()
        label.text = "Header"
        return label
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableview.dequeueReusableCell(withIdentifier: CustomCell.identifier, for: indexPath) as! CustomCell
        return cell

    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80.0
    }
}

标签: iosiphoneswiftuitableviewuiscrollview

解决方案


UITextField您的单元格一旦超出表格视图的可见范围就会从内存中删除,因此不能期望它们保留数据(您的内容)。

您必须查看UITableViewDataSource协议并将单元格的内容存储UITextFields在一个类中,该类将在您的表格视图的视图控制器的持续时间内保留在内存中。

通常,人们使用视图控制器作为数据源,就像您所做的那样。

步骤如下:

  • 在视图控制器的初始化中,创建并准备一个数据结构(以 IndexPaths 为键的数组/字典),它将包含您需要存储的文本内容
  • 将单元格出列时(在您的cellForRowAt函数中),如果该特定 indexPath 的内容存在,请使用数据结构中的必要字符串配置单元格。
  • 当用户在单元格中更改文本时,通知您的数据源单元格索引路径的新内容

例子:

定义以下协议:

protocol DataSourceUpdateDelegate: class {
    func didUpdateDataIn(_ sender: UITableViewCell, with text: String?)
}

确保您UITableViewCell声明一个委托变量并使用它:

class MyCell: UITableViewCell, UITextFieldDelegate {
    @IBOutlet var myTextField: UITextField!
    weak var dataSourceDelegate: DataSourceUpdateDelegate?

    func configureCellWithData(_ data: String?, delegate: DataSourceUpdateDelegate?)
    {
        myTextField.text = data
        myTextField.delegate = self
        dataSourceDelegate = delegate
    }

    override func prepareForReuse() {
        myTextField.text = ""
        super.prepareForReuse()
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        dataSourceDelegate?.didUpdateDataIn(self, with: textField.text)
    }
}

确保您的 View Controller 符合DataSourceUpdateDelegate并初始化一个变量来管理数据:

class MyViewController: UITableViewController, UITableViewDataSource, DataSourceUpdateDelegate {
    var data = [IndexPath : String]()

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = MyCell() // Dequeue your cell here instead of instantiating it like this
        let cellData = data[indexPath]
        cell.configureCellWithData(cellData, delegate: self)
        return cell
    }

    func didUpdateDataIn(_ sender: UITableViewCell, with text: String?) {
        data[tableView.indexPath(for: sender)!] = text
    }
}

推荐阅读