我的tableView有两个原型单元格,在故事板中编程如下。
标识符都是正确的另一个不是IB和故事板之间的错误,因为标识符链接起来。但是当我构建并运行项目时,我会看到以下屏幕(请注意,在此运行中,semester.subjects和semester.activities为空)。
另外请注意,当我点击我的第一个单元格时,没有任何反应,但是当我点击我的第二个单元格时,它会改变为我想要的那个。我的问题是这是怎么发生的?如果需要,下面是我的cellForRowAt indexPath。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "subjectCell", for: indexPath) as! SubjectTableViewCell
let addCell = tableView.dequeueReusableCell(withIdentifier: "addCell", for: indexPath) as! AddSubjectTableViewCell
var cellToAdd = UITableViewCell()
switch indexPath.section {
case 0:
if indexPath.row < semester.subjects.count{
let subject = semester.subjects[indexPath.row]
cell.subjectColourView.backgroundColor = subject.colour
cell.subjectLabel.text = subject.name
cell.teacherLabel.text = subject.teacher
cellToAdd = cell
}
else if indexPath.row == semester.subjects.count {
cellToAdd = addCell
}
case 1:
if indexPath.row < semester.activities.count{
let activity = semester.activities[indexPath.row]
cell.subjectColourView.backgroundColor = activity.colour
cell.subjectLabel.text = activity.name
cell.teacherLabel.text = activity.teacher
cellToAdd = cell
}
else if indexPath.row == semester.activities.count {
cellToAdd = addCell
}
default:
break
}
print(cellToAdd.reuseIdentifier)
return cellToAdd
}
这是整个TVC
import UIKit
类SubjectTableViewController:UITableViewController { var semester:学期!
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return semester.subjects.count + 1
case 1:
return semester.activities.count + 1
default:
return 0
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return "Subjects"
case 1:
return "Extracurricular Activities"
default:
return nil
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "subjectCell", for: indexPath) as! SubjectTableViewCell
let addCell = tableView.dequeueReusableCell(withIdentifier: "addCell", for: indexPath) as! AddSubjectTableViewCell
var cellToAdd = UITableViewCell()
switch indexPath.section {
case 0:
if indexPath.row < semester.subjects.count{
let subject = semester.subjects[indexPath.row]
cell.subjectColourView.backgroundColor = subject.colour
cell.subjectLabel.text = subject.name
cell.teacherLabel.text = subject.teacher
cellToAdd = cell
}
else if indexPath.row == semester.subjects.count {
cellToAdd = addCell
}
case 1:
if indexPath.row < semester.activities.count{
let activity = semester.activities[indexPath.row]
cell.subjectColourView.backgroundColor = activity.colour
cell.subjectLabel.text = activity.name
cell.teacherLabel.text = activity.teacher
cellToAdd = cell
}
else if indexPath.row == semester.activities.count {
cellToAdd = addCell
}
default:
break
}
print(cellToAdd.reuseIdentifier)
return cellToAdd
}
}
答案 0 :(得分:0)
问题是,谁知道是什么原因,那个单元在开关外被拒绝了。
if indexPath.row < semester.subjects.count{
let cell = tableView.dequeueReusableCell(withIdentifier: "subjectCell", for: indexPath) as! SubjectTableViewCell
let subject = semester.subjects[indexPath.row]
cell.subjectColourView.backgroundColor = subject.colour
cell.subjectLabel.text = subject.name
cell.teacherLabel.text = subject.teacher
cellToAdd = cell
}