ios - 将单元格移动到表格视图中的新部分
问题描述
用户选择按钮后,如何将单元格从表格视图中的第一部分移动到第二部分?
这建立了我的表格视图。
@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()
}
}
推荐阅读
- javascript - flexbox 中单行中的项目数
- leaflet - 在事件中重新设置整个传单矢量地图的样式
- r - 如何为数据集的每个值制作唯一的数据集
- java - 一些插件间依赖没有得到满足
- mysql - NodeJS sequelize 自动生成模型并运行迁移 SQL 语法错误
- ruby-on-rails - 将 UTC 转换为 EST 并以 EST 格式保存在数据库中
- javascript - 如何从不同的 php 文件中获取另一个变量到另一个文件中?
- html - 仅在高度而不是宽度上更改图像的大小
- html - CSS网格最小跨度3,如果类对象只是网格行中的对象,则填充行中的所有网格元素
- java - 如何处理并发数据库请求或如何在长时间运行的查询正在进行时运行简短的 sql 查询