【问题标题】:Scrolling Tableview data reshuffle in UITextfield in Swift在 Swift 的 UITextfield 中滚动 Tableview 数据重新洗牌
【发布时间】:2019-05-27 09:41:36
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios iphone swift uitableview uiscrollview


    【解决方案1】:

    不能期望您的单元格保留数据(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
        }
    }
    

    【讨论】:

    • 我使用相同的程序并通过帮助此链接stackoverflow.com/a/41707184/7405878 修复了一个部分行中的问题,但当我使用多个部分时没有。
    • @link786 该解决方案定义了一个单维数组来存储单元格数据。由于您有多个部分,因此以 indexPath 为键的字典会更好。然而,最好的解决方案是让单元格有一个自定义委托,它会通知数据源有关更改。
    • 我试过了,但我似乎无法弄清楚。你能给我一些示例代码吗?会很棒!
    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2016-07-02
    • 2016-04-26
    • 1970-01-01
    相关资源
    最近更新 更多