ios - 将单元格移动到表格 View 中的新部分

标签 ios swift xcode uitableview

用户选择按钮后,如何将单元格从表格 View 中的第一部分移动到第二部分?

这建立了我的表格 View 。

    @IBOutlet weak var goalTableView: UITableView!

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

    override func viewDidLoad() {
        super.viewDidLoad()

在我的 viewdidload 中,我会添加一个从按钮到我的 View Controller 的连接,但是我是否必须在这里开发一个函数来将选定的单元格移动到新部分?
        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) {
    }
}

最佳答案

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()
    }
}

关于ios - 将单元格移动到表格 View 中的新部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61681471/

相关文章:

ios - 无法使用 CGRectMake 调整 UIButton 的大小

ios - UITabBar 不改变色调 Xcode 9.3

ios - NSObject 和 AnyObject 有什么区别?什么时候用这两个?

ios - 在 iOS 7 中始终获得唯一的设备 ID

ios - Xcode 可以支持多种布局吗?

ios - 快速在 TableView 内的 Collection View 中显示内容时出现问题

ios - 如何在 Realm swift 中使用 LIKE/CONTAINS 查询双值

ios - 向使用 Fetch Controller 的 TableView 添加编辑行操作?

ios - 可重复使用的 Collection View 单元显示奇怪的行为

ios - 数组元素连续追加