将单元格移动到表视图中的新分区



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

这建立了我的表视图。

@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中,我会添加一个从按钮到视图控制器的连接,但我是否必须在这里开发一个函数来将所选单元格移动到新的部分?

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

最新更新