Итак, я давно программировал на 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"
, он ничего не загружает и не показывает. Он также не будет подтягивать ярлык, как будто он не существует. Я так растерялся.