Swift 自定义TableViewController生成空白单元格
我一直在关注苹果的Swift教育书籍,我正在创建一个待办事项列表。我创造了自己的,有这个问题,所以我撕毁了我所有的代码,直到我得到了一个完全相同的副本,一行一行的苹果 它使用我的TableViewController子类生成空白单元格Swift 自定义TableViewController生成空白单元格,swift,list,tableview,cells,Swift,List,Tableview,Cells,我一直在关注苹果的Swift教育书籍,我正在创建一个待办事项列表。我创造了自己的,有这个问题,所以我撕毁了我所有的代码,直到我得到了一个完全相同的副本,一行一行的苹果 它使用我的TableViewController子类生成空白单元格 class ToDoTableViewController: UITableViewController { var todos = [ToDo]() 在我的TableViewController(在情节提要中链接)中,有两个覆盖;一个退出队列,另一个返
class ToDoTableViewController: UITableViewController {
var todos = [ToDo]()
在我的TableViewController(在情节提要中链接)中,有两个覆盖;一个退出队列,另一个返回一个部分中所有行的行数
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return todos.count
}
override func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
guard let cell =
tableView.dequeueReusableCell(withIdentifier:
"ToDoCellIdentifier") else {
fatalError("Could not dequeue a cell")
}
let todo = todos[indexPath.row]
cell.textLabel?.text = todo.title
return cell
}
然后是viewDidLoad覆盖,以便插入示例项
override func viewDidLoad() {
super.viewDidLoad()
if let savedToDos = ToDo.loadToDos() {
todos = savedToDos
} else {
ToDo.loadSampleToDos()
}
}
Xcode说这些是未使用的
我的另一个文件是ToDo类,包含标题、截止日期等以及我的示例数据:
class ToDo: NSObject {
var title: String
var isComplete: Bool
var dueDate: Date
var notes: String?
init(title: String, isComplete: Bool, dueDate: Date, notes:
String?) {
guard !title.isEmpty else {
fatalError("Reminder requires a non-empty title")
}
self.title = title
self.isComplete = isComplete
self.dueDate = dueDate
self.notes = notes
}
以及文件中的示例数据:
static func loadSampleToDos() -> [ToDo] {
let todo1 = ToDo(title: "ToDo One", isComplete: false,
dueDate: Date(), notes: "Notes 1")
let todo2 = ToDo(title: "ToDo Two", isComplete: false,
dueDate: Date(), notes: "Notes 2")
let todo3 = ToDo(title: "ToDo Three", isComplete: false,
dueDate: Date(), notes: "Notes 3")
return [todo1, todo2, todo3]
}
我已在情节提要中将单元格标识符设置为“ToDoCellidentier”
运行它,只有空白单元格。为什么它们是空白的?试试这个
override func viewDidLoad() {
super.viewDidLoad()
if let savedToDos = ToDo.loadToDos() {
todos = savedToDos
} else {
todos = ToDo.loadSampleToDos()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todos.count
}
在单元格中显示todo值的位置?你用完整的
cellForRowAt
@Ragul提问是什么意思?var todos=[ToDo]()cell.textLabel?.text=“此处显示文本”在为todosStill赋值后重新加载tableView,直到生成空白单元格。我正在使用Swift 4的开发者预览版,如果这对我的情况有影响的话……您是否实现了tableView(\utableview:UITableView,numberofrowsinssection:Int)
?发布更多的问题代码可以帮助我们发现问题@Yeeeeesorry..什么是OP?原始帖子。但我刚刚看到了类ToToToTableViewController:UITableViewController{var todos=[ToDo]()