【问题标题】:ULTextField values disappears when the device orientation changes设备方向更改时 ULTextField 值消失
【发布时间】:2020-09-29 08:49:03
【问题描述】:

我正在使用 Xibs 加载 UITableViewController 中的单元格,并且由于某种原因,一旦设备方向发生变化,单元格就会重新加载,并且所有输入的 UITextField 值都会消失。有没有办法防止这种情况或更好的方法来解决价值消失的问题。

我的单元格如下

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = configCellView(index: indexPath.row) as? UITableViewCell

    cell?.selectionStyle = .none
    return cell ?? UITableViewCell()
}

我的 configCellView 方法如下

   private func configCellView(index: Int) -> UIView? {
    switch formType {
    //This LOG form works fine even device orientation changes 
    case .LOG:
        if let cell = getXibFile(cellType: LogFormTableViewCell.self, cellIdenifier: Constants.LOGFORM_CELL_IDENTIFIER) {
        
        return cell

        }

  //This is where the issue is 
    case .PROSPECT:
        switch index {
            case 0:
                let cell = getXibFile(cellType: LogFormTableViewCell.self, cellIdenifier: Constants.LOGFORM_CELL_IDENTIFIER)

                return cell
            default:
               
               
                    if let cell = getXibFile(cellType: ContactFormTableViewCell.self, cellIdenifier: Constants.CONTACTFORM_CELL_IDENTIFIER){
                                                 
                  
                     return cell
                
                    }
        }

     }



 func getXibFile<T: UITableViewCell>(cellType: T.Type, cellIdenifier: String) -> T? {
    return Bundle.main.loadNibNamed(cellIdenifier, owner: self, options: nil)?.first as? T
 }

【问题讨论】:

  • 保存输入的值,如果已经保存的话,在tableView(_:, cellForRowAt:)里放一个..
  • 静态持久化它们我不认为最好的选择是消耗内存
  • 将字符串保存到 var(或字符串数​​组)中,应该没问题。

标签: swift uitableview xib swift5


【解决方案1】:

您当前所做的是每次重新加载 tableView 时创建一个新单元格,这是不推荐的。您必须dequeueReusableCell(withIdentifier:) 才能重复使用单元格,这在内存方面更有效。

关于数据,您可能希望每次调用cellForRow 时都有一个数组来存储和检索数据。

【讨论】:

  • 如果你正确阅读我的问题,我相信 dequeueReusableCell 是用于原型单元而不是 Xibs
  • 不,重用也适用于 xib。我没有看到,但很明显,你应该重用(出队)。这不是你的主要问题,但这是一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多