Перемещение ячеек в новые разделы в табличном представлении - PullRequest
2 голосов
/ 08 мая 2020

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

Это устанавливает мое представление таблицы.

    @IBOutlet weak var goalTableView: UITableView!

    let sections: [String] = ["Today:", "History:"]
    var goals: [[String]] = [["Goal 1", "Goal 2", "Goal 3"], [""]]

    override func viewDidLoad() {
        super.viewDidLoad()

В моем представлении добавит соединение от моей кнопки к моему контроллеру представления, но нужно ли мне разработать здесь функцию, чтобы переместить выбранную ячейку в новый раздел?

        let headerView = UIView()
        goalTableView.tableHeaderView = headerView
        headerView.frame = CGRect(x: 0, y: 0, width:   view.frame.width, height: 5)
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return goals[section].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt   indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodayGoalViewCell_1", for: indexPath) as? GoalTableViewCell
        cell?.goalLabel.text = goals[indexPath.section][indexPath.row]
        cell?.cellDelegate = self
        cell?.index = indexPath
        return cell!
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return goals.count
    }
}

extension ViewController: GoalTableView {
    func selectGoalButton(index: Int) {
    }
}

1 Ответ

1 голос
/ 08 мая 2020
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // Only move cell from the 1st section
    if indexPath.section == 0 {

        // Append the selected element to the second array
        goals[1].append(goals[0][indexPath.row])

        // Remove the selected element from the first array
        goals[0].remove(at: indexPath.row)

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