Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift中UITextfield中的滚动Tableview数据重新排列_Ios_Iphone_Swift_Uitableview_Uiscrollview - Fatal编程技术网

Ios Swift中UITextfield中的滚动Tableview数据重新排列

Ios Swift中UITextfield中的滚动Tableview数据重新排列,ios,iphone,swift,uitableview,uiscrollview,Ios,Iphone,Swift,Uitableview,Uiscrollview,我有一个表格视图,其中单元格由多个部分组成,每个部分有不同的行数,每行有一个文本字段。当我在里面写东西,上下滚动时,数据丢失或被重新整理。所以我试图将文本字段数据保存到二维数组中,但我无法解决这个问题 以下是自定义单元格中的代码: class CustomCell: UITableViewCell { @IBOutlet weak var userName: UITextField! override func awakeFromNib() { super.aw

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

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

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
    }
}

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

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

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

步骤如下:

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

定义以下协议:

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)
    }
}
确保视图控制器符合DataSourceUpdateLegate,并初始化变量以管理数据:

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
    }
}

我使用了相同的过程,并通过帮助此链接在一节行中发布了“修复”,但在使用“多节”时没有。@link786该解决方案定义了一个一维数组来存储单元格数据。因为您有多个部分,所以基于indexPath的字典会更好。然而,最好的解决方案是让单元格有一个自定义委托,它将更改通知数据源。你能给我一些示例代码吗?那太好了!