搜索后,我找不到适合我的解决方案。我在表格单元格中有几个文本字段。我试图完成的是使用前导滑动将这些字段从隐藏更改为可见。
cellForRowAt
代码:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
let newCategory = cell.viewWithTag(5) as! UITextField
let newAmount = cell.viewWithTag(6) as! UITextField
newCategory.isHidden = true
newAmount.isHidden = true
return cell
}
行距代码滑动:
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath)
let contextItem = UIContextualAction(style: .normal, title: "Edit") { (contextualAction, view, boolValue) in
cell.newCategory.isHidden = false
cell.newAmount.isHidden = false
boolValue(false)
}
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])
return swipeActions
}
我收到错误"类型为'UITableViewCell'的值没有成员'newCategory'"和类似的新金额。
将let newCategory = cell.viewWithTag(5) as! UITextField
和let newAmount = cell.viewWithTag(6) as! UITextField
移动到前导滑动功能中即可完成这项工作。
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let cell = tableView.cellForRow(at: indexPath)
let newCategory = cell.viewWithTag(5) as! UITextField
let newAmount = cell.viewWithTag(6) as! UITextField
let contextItem = UIContextualAction(style: .normal, title: "Edit") { (contextualAction, view, boolValue) in
newCategory.isHidden = false
newAmount.isHidden = false
boolValue(false)
}
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])
return swipeActions
}