Создание другого TableViewCell при нажатии на кнопку - PullRequest
0 голосов
/ 01 апреля 2020
let cell = tableView.dequeueReusableCell(withIdentifier: CellButton.identifier(), for: indexPath) as! CellButton
            cell.build(height: 40, color: .lighterGray())
            cell.blockAction = {
                let category = categories?[indexPath.row]
                let cell = tableView.dequeueReusableCell(withIdentifier: 
             CellCheck.identifier(), for: indexPath) as! 
            CellCheck 
           }
 return cell

Это код, который у меня есть, и когда вызывается blockAction (функция вызывается, когда я нажимаю кнопку в ячейке), мне нужно создать еще один TableViewCell и удалить сначала появившиеся

Есть ли способ загрузить его внутри той же ячейки, а также увеличить высоту на высоту динам c содержимое ячейки

1 Ответ

0 голосов
/ 02 апреля 2020

Вы можете удалить IndexPath, а затем добавить добавить новый indexPath.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableView: UITableView!

private var buttonTapped: Bool = false
private var selectedIndexPath: IndexPath?

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if buttonTapped == true,
        let olderIndexPath = selectedIndexPath,
        selectedIndexPath == olderIndexPath {

        let cell = UITableViewCell()
        cell.backgroundColor = .red
        return cell
    }

    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

    cell.callback = {[weak self] in
        self?.buttonTapped = true
        self?.selectedIndexPath = indexPath
        self?.insertNewCell(indexPath)
    }
    return cell
}

private func insertNewCell(_ indexPath: IndexPath) {
    tableView.beginUpdates()
    tableView.deleteRows(at: [indexPath], with: .right)
    tableView.insertRows(at: [indexPath], with: .left)
    tableView.endUpdates()

}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...