Как добавить ячейку табличного представления с кнопкой внутри другой ячейки табличного представления? - PullRequest
0 голосов
/ 26 июня 2019

Я пытаюсь создать приложение со списком дел, и у меня возникают проблемы при попытке добавить подзадачи к моим основным задачам.У меня есть кнопка внутри каждой ячейки табличного представления, когда эта кнопка нажата, я хочу добавить еще одну ячейку табличного представления, в которую я могу напечатать и сохранить «подзадачу».

    //set up tableView row amount
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return taskData.count
    }

    //set up what is in the tableView row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //This is for a main task
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! CustomTableViewCell

        cell.label.text = taskData[indexPath.row]

        return cell
    }

1 Ответ

0 голосов
/ 26 июня 2019

Использование делегатов для передачи данных из ячеек в ваш ViewController и перезагрузки tableView.

CustomTableViewCell:

protocol CustomTableViewCellDelegate: class {
   func customTableViewCellButtonClicked(_ cell: CustomTableViewCell)
}

class CustomTableViewCell: UITableViewCell {
   weak var delegate: CustomTableViewCellDelegate?

   func buttonClicked() {
       self.delegate?.customTableViewCellButtonClicked(self)
   }
}

ViewController:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! CustomTableViewCell
    cell.label.text = taskData[indexPath.row]
    cell.delegate = self

    return cell
}

func customTableViewCellButtonClicked(_ cell: CustomTableViewCell) {
     // add the task you need from that cell to your tasks list.
     taskData.append(....)
     //reload your tableView
     self.tableView.reloadData()
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...