搜索后,我找不到适合我的解决方案。我在表格单元格中有几个文本字段。我要完成的工作是使用LeadingSwipe将这些字段从隐藏更改为可见。
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
}
leadingSwipe的代码:
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””和newAmount的成员。
答案 0 :(得分:0)
将let newCategory = cell.viewWithTag(5) as! UITextField
和let newAmount = cell.viewWithTag(6) as! UITextField
移动到leadingSwipe函数中即可完成工作。
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
}