自定义UITableViewCell类不起作用(Swift)



所以我用swift编程已经有一段时间了,我只想说,它过去比现在容易得多。

无论如何,我到处寻找答案,但仍然没有找到。我有一个UITableView控制器,它位于父视图控制器的容器中。该表视图控制器称为RecentActivityTableViewController。我的最终目标是更改UITableViewCell类中包含的标签上的文本。所有内容都已连接,并且类已设置为视图控制器

private let appDelegate = UIApplication.shared.delegate as! AppDelegate
private let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
private var shifts = [Shift]()
private var clock = [Clock]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(RecentTableViewCell.classForCoder(), forCellReuseIdentifier: "recentCell")
// 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 viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
do {
try shifts = context.fetch(Shift.fetchRequest())
print("Loaded items in the substrate! ;)")
} catch let error as NSError {
print("Could not load, error :(  (error)")
}
do {
try clock = context.fetch(Clock.fetchRequest())
print("Loaded clock in!!!! ;)")
} catch let error as NSError {
print("Could not load, error :( (error), (error.userInfo)")
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("running table")
var cell: RecentTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "recentCell", for: indexPath) as! RecentTableViewCell
if cell == nil {
tableView.register(UINib(nibName: "RecentTableViewCell", bundle: nil), forCellReuseIdentifier: "recentCell")
cell = tableView.dequeueReusableCell(withIdentifier: "recentCell") as? RecentTableViewCell
}
return cell
}
public func addShift(){
if(!appDelegate.clockedIn || appDelegate.clockedIn == nil){
let clocked = Clock(entity: Clock.entity(), insertInto: context)
clocked.clockIn = Date()
clocked.name = "Ethan Jamieson"
self.appDelegate.saveContext()
}
else{
let shift = Shift(entity: Shift.entity(), insertInto: context)
shift.punchIn = self.clock[1].clockIn
shift.punchOut = Date()
shift.name = self.clock[1].name
self.appDelegate.saveContext()
}
}

我的自定义单元格视图控制器看起来像这个

class RecentTableViewCell: UITableViewCell {
@IBOutlet weak var test: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}

}

我最后一次记得如何更改单元格中的项目是拉起视图控制器并更改其中的内容

cell.test.text = "custom text"

它不加载或显示任何内容。它也不会像不存在一样拉起标签。我很困惑。

这些是我能看到的问题:

viewDidLoad中,您的代码应该是:

self.tableView.register(RecentTableViewCell.self, forCellReuseIdentifier: "recentCell")

cellForRowAt:中

let cell = tableView.dequeueReusableCell(withIdentifier: "recentCell", for: indexPath) as! RecentTableViewCell
cell.test.text = "custom text"
return cell

如果单元格不是可选单元格,就没有理由检查它是否为零。

cellForRowAt中的return cell中,将其更改为return cell!

最新更新