原型单元格不能正确显示
问题描述:
我的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 学期:学期!
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
}
}
答
问题是,谁知道是什么原因,该单元在交换机之外被拒绝。
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
}
请在您的问题中添加更多代码,我们可以轻松测试您的代码,您只需添加一部分代码即可。 – aircraft
@aircraft你想要什么? – needshelp
请为您添加'didSelecteCellAtIndexPath'代码。另外,请描述你想要的行为,因为它并不是完全显而易见的结果。 – Erik