我有一个自定义tableViewCell
,textField
只有一个开关,它不是tableViewCell
的一部分。
所以我想在打开和关闭时做,然后我的tableViewCell
textFields
被禁用和启用。那么我该怎么做呢?
@IBAction func switchServiceTax_Action(_ sender: UISwitch) {
let cell : tbleCell = tblViewServices.dequeueReusableCell(withIdentifier: "cell") as! tbleCell
if switch.isOn{
cell.txtFld.isUserInteractionEnabled = true
tblView.reloadData()
}else{
cell.txtFld.isUserInteractionEnabled = false
tblView.reloadData()
}
}
有两种方法可以实现此要求。
- 在
cellforRow
中,您可以直接使用下面的行,而不是if-else
并确保每次切换开关时重新加载表视图。
cell.txtFld.isUserInteractionEnabled = switch.isOn
-
只需在UITextFieldDelgate下面实现,仅此而已。无需重新加载表视图,因此可以提高应用程序性能。
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { return switch.isOn }
您可以使用单元格中的切换对象进行行。这是简单的逻辑写在下面并按照说明进行操作。
@IBAction weak var switchTextField: UISwitch!
@IBAction func switchServiceTax_Action(_ sender: UISwitch) {
tblView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TBLcell
cell.txtFirstName.resignFirstResponder()
cell.txtFirstName.isUserInteractionEnabled = switchTextField.isOn
return cell
}