好吧,我正在尝试制作一份注册表。我的问题是,该节的单元格未在各节中显示正确的信息,而是在各节中显示了所有信息...嗯,我认为我在行单元格中错过了一些东西。
所以,我的代码看起来像这样。
UITableViewDataSource
extension IEKRegistrationPage3Controller: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "IEKRegistrationPage3GraduateCell", for: indexPath) as! IEKRegistrationPage3GraduateCell
// Graduate
if let graduate = Graduate(rawValue: indexPath.row) {
cell.updateLayout(withTitle: graduate.description, isSelected: [graduate] == iekRegistrationFormData.graduate)
}
if let student = Student.init(rawValue: indexPath.row) {
cell.updateLayout(withTitle: student.description, isSelected: [student] == iekRegistrationFormData.student)
}
return cell
}
}
这也是 UITableViewDelegate
extension IEKRegistrationPage3Controller: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Graduate
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.section == 0 {
if let graduate = Graduate(rawValue: indexPath.row) {
iekRegistrationFormData.graduate = [graduate]
}
}
// Student
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.section == 1 {
if let student = Student(rawValue: indexPath.row) {
iekRegistrationFormData.student = [student]
}
}
// Engilsh
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.section == 2 {
// TODO
}
tableView.reloadData()
}
}
,最后是 titleForHeaderInSection
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// Graduate
if section == 0 {
return "ΑΠΟΦΟΙΤΟΣ"
}
// Student
if section == 1 {
return "ΦΟΙΤΗΤΗΣ"
}
// Engilsh
if section == 2 {
return "ΑΓΓΛΙΚΑ"
}
return nil
}