我有以下代码,但我无法填充TableViewCell。请帮忙
这是模型文件Leave.swift
import UIKit
class Leave {
var startDate: Date!
var endDate: Date!
var description: String?
init?(startDate: Date, endDate: Date, description: String) {
// start date can never ever be later than end date
if startDate > endDate {
return nil
}
self.startDate = startDate
self.endDate = endDate
self.description = description
}
}
我有以下LeaveTableViewController.swift
import UIKit
class LeaveTableViewController: UITableViewController {
var leaves = [Leave]()
private func loadUpcomingLeave() {
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.timeStyle = .none
let startDate1 = formatter.date(from: "July 5, 2018")
let endDate1 = formatter.date(from: "July 6, 2018")
guard let leave1 = Leave(startDate: startDate1!, endDate: endDate1!, description: "testing") else {
fatalError("Unable to instantiate leave1")
}
let startDate2 = formatter.date(from: "August 5, 2018")
let endDate2 = formatter.date(from: "August 7, 2018")
guard let leave2 = Leave(startDate: startDate2!, endDate: endDate2!, description: "testing") else {
fatalError("Unable to instantiate leave1")
}
self.leaves += [leave1, leave2]
}
override func viewDidLoad() {
super.viewDidLoad()
// Load the sample data
loadUpcomingLeave()
self.tableView.register(LeaveTableViewCell.self, forCellReuseIdentifier: "LeaveTableViewCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.leaves.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Table view cels are reused and should be dequeued using a cell idenifier
let cellIdentifier = "LeaveTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? LeaveTableViewCell else {
fatalError("The dequeued cell is not an instance of LeaveTableViewCell.")
}
let leave = self.leaves[indexPath.row]
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .none
cell.startDateLabel?.text = df.string(from: leave.startDate)
cell.endDateLabel?.text = df.string(from: leave.endDate)
return cell
}
}
当我运行应用程序时,它会显示表格视图但不显示任何单元格数据。我期待2个休假角色,包括开始日期和结束日期。我错过了什么?
答案 0 :(得分:0)
案例1 - 检查数据是否存在并从离开CLass
出来案例2 - 如果数据来自班级
override func viewDidLoad() {
super.viewDidLoad()
//Try using this I think nib you created is not getting registered properly
self.tableView.register(UINib(nibName: "LeaveTableViewCell", bundle: nil), forCellReuseIdentifier: "LeaveTableViewCell")
loadUpcomingLeave()
self.tableView.reloadData()
}
答案 1 :(得分:0)
试试这个
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(LeaveTableViewCell.self, forCellReuseIdentifier: "LeaveTableViewCell")
// Load the sample data
loadUpcomingLeave()
}
private func loadUpcomingLeave() {
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.timeStyle = .none
let startDate1 = formatter.date(from: "July 5, 2018")
let endDate1 = formatter.date(from: "July 6, 2018")
guard let leave1 = Leave(startDate: startDate1!, endDate: endDate1!, description: "testing") else {
fatalError("Unable to instantiate leave1")
}
let startDate2 = formatter.date(from: "August 5, 2018")
let endDate2 = formatter.date(from: "August 7, 2018")
guard let leave2 = Leave(startDate: startDate2!, endDate: endDate2!, description: "testing") else {
fatalError("Unable to instantiate leave1")
}
self.leaves += [leave1, leave2]
self.tableView.reloadData()
}
答案 2 :(得分:0)