【发布时间】: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