Im using Xibs to load cells inside a UITableViewController, and for some reason once the device orientation changes the cell is getting reloads and all the entered UITextField values get disappear. Is there a way to prevent this or better way to solve this problem of values getting disappeared.
my cell for row as follow
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = configCellView(index: indexPath.row) as? UITableViewCell
cell?.selectionStyle = .none
return cell ?? UITableViewCell()
}
My configCellView method as follow
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
}
What you are currently doing is creating a new cell every time the tableView reload which is not recommended. You have to
dequeueReusableCell(withIdentifier:)so that cell can be reused, which is more efficient in terms of memory.Regarding data, you might want to have an array to store and retrieve data every time
cellForRowget called.