如何访问位于单元格外部的表格视图单元格中的文本字段?

时间:2018-09-16 21:27:49

标签: ios swift uitableview

搜索后,我找不到适合我的解决方案。我在表格单元格中有几个文本字段。我要完成的工作是使用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的成员。

1 个答案:

答案 0 :(得分:0)

let newCategory = cell.viewWithTag(5) as! UITextFieldlet 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
}