我有一个注册PaymentAddressTableViewCell
的UITableView,它有一个名为validate
的方法,它查看cellType
枚举,并对单元格中的文本字段应用特定的验证。
let tableView = UITableView()
tableView.register(PaymentAddressTableViewCell.self, forCellReuseIdentifier: "PaymentAddressCell")
tableView.dataSource = self
tableView.delegate = self
... more layout
我在Fabric中看到有时下面的演员会失败。到目前为止,我已将其归结为UIKit内部失败的内容。它也可能是对行号的误报,因为应用程序是为生产而编译的。但是为了这个,让我们假设它不是。
我开始将力展开更改为guard let
,如下所示。那个案子应该永远不会真的发生。施法非常简单......
for i in [0, 1, 2, 3, 4] {
let indexPath = IndexPath(row: i, section: 1)
guard let cell = tableView.cellForRow(at: indexPath) as? PaymentAddressTableViewCell else {
... this should never really be hit
}
}
知道这可能是什么,或者我如何避免演员失败?
谢谢
修改 cellForRowAtIndexPath
代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PaymentAddressCell") as! PaymentAddressTableViewCell
cell.textField?.returnKeyType = .done
cell.textField?.delegate = self
switch (indexPath.section, indexPath.row) {
case (0, 0):
cell.fieldType = .cardNumber
cell.textField?.placeholder = "5555123456789999"
cell.textField?.keyboardType = .numberPad
cell.textField?.addTarget(self, action: #selector(cardNumberChanged(_:)), for: .editingChanged)
case (0, 1):
cell.fieldType = .expiration
cell.textField?.placeholder = "01/20"
case (0, 2):
cell.fieldType = .cvv
cell.textField?.placeholder = "CVV"
cell.textField?.keyboardType = .numberPad
cell.textField?.addTarget(self, action: #selector(cvvChanged(_:)), for: .editingChanged)
case (1, 0):
cell.fieldType = .address
cell.textField?.placeholder = "123 Main Street"
cell.textField?.autocapitalizationType = .words
cell.textField?.keyboardType = .default
case (1, 1): // address line 2
cell.textField?.placeholder = "Apt 3"
cell.textField?.autocapitalizationType = .words
cell.textField?.keyboardType = .default
case (1, 2):
cell.fieldType = .city
cell.textField?.placeholder = "City"
cell.textField?.autocapitalizationType = .words
cell.textField?.keyboardType = .default
case (1, 3):
let dict: [(key: String, value: String)] = viewModel.statePickerOptions.sorted(by: {
return $0.0 < $1.0
})
cell.textField?.placeholder = "State"
cell.statesDataSource = dict
cell.fieldType = .state
case (1, 4):
cell.fieldType = .zipcode
cell.textField?.placeholder = "Zipcode"
cell.textField?.keyboardType = .numberPad
default: break
}
return cell
}