Как получить доступ к текстовому полю, расположенному в ячейке табличного представления вне ячейки? - PullRequest
0 голосов
/ 17 сентября 2018

После поиска я не смог найти решение, которое бы сработало для меня. У меня есть несколько текстовых полей в ячейке таблицы. То, что я пытаюсь сделать, это изменить эти поля со скрытых на видимые с помощью начального стирания.

Код для 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
}

Код для ведущихSwipe:

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 голосов
/ 17 сентября 2018

Перемещение let newCategory = cell.viewWithTag(5) as! UITextField и let newAmount = cell.viewWithTag(6) as! UITextField в функцию leadSwipe сделало свою работу.

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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...