【发布时间】:2020-05-08 22:34:13
【问题描述】:
现在我在表view中有两个部分,并且选择某个行时,它会移动到另一个部分。有没有办法在没有单元格占用该部分时显示诸如“视图为空”之类的消息,然后当单元格确实占用该部分时,该消息被清除?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var goalTableView: UITableView!
let sections: [String] = ["Mark as Complete:", "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
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
progressBarAnimation()
if indexPath.section == 0 {
goals[1].append(goals[0][indexPath.row])
goals[0].remove(at: indexPath.row)
tableView.reloadData()
}
}
}
extension ViewController: GoalTableView {
func selectGoalButton(index: Int) {
}
}
【问题讨论】:
-
您能否详细说明您希望它在选择单元格时完成什么?代码不是很清楚。从我所见,您似乎希望第一部分中的单元格清除第二部分中的匹配行?
-
代码已经将一个单元格从一个部分移动到另一个部分,但目前在程序开始时,部分 2 是空白的。在这个空白部分,我想显示一条消息。然后,一旦用户从第一个单元格录制了一行,该单元格将移动到第二个部分。一旦发生这种情况,最初在第 2 节中的消息应该会消失。