【发布时间】:2023-03-20 11:30:01
【问题描述】:
在用户选择按钮后,如何将单元格从表格视图中的第一部分移动到第二部分?
这建立了我的表格视图。
@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) {
}
}
【问题讨论】:
-
如果您正在选择要移动的单元格,那么为什么不使用 didSelectRowAt 呢?我不明白你想用一个单独的按钮来实现什么?
-
一旦用户单击单元格中的按钮,我希望该单元格自动转到第二部分,因为该按钮也已经连接到进度条。所以用户只需要做出一个选择。如果我使用 didSelectRowAt 会影响用户选择单元格的次数吗?
-
除非您的单元格中正在进行其他 UI 事件,否则我看不出您为什么不能使用 didSelectRowAt。我会尽快为您发布解决方案。
标签: ios swift xcode uitableview