I have several common "table views" with cells having the same name: "LiveViewCell"
, which I would like to reload with data created in a view controller
.
Only the next view controller
gets the reloadData()
call as a notification
, and not even the current one sending the data gets it.
This is the code I implemented in all view controllers:
@IBOutlet weak var tableViewLiveCells: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadLive),name:NSNotification.Name(rawValue: "load"), object: nil)
}
func reloadLive(notification: NSNotification) {
tableViewLiveCells.reloadData()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return NT.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewLiveCells.dequeueReusableCell(withIdentifier: "LiveViewCell", for: indexPath) as! LiveViewCell
let tab: Table!
tab = NT[indexPath.row]
cell.configureLiveCell(tab)
return cell
}
This is the LiveViewCell.swift
file
import UIKit
class LiveViewCell: UITableViewCell {
@IBOutlet weak var tableNumberLeftLabel: UILabel!
@IBOutlet weak var guestNumbersLabel: UILabel!
@IBOutlet weak var timeInTableLabel: UILabel!
@IBOutlet weak var tableNumberLbl: UILabel!
@IBOutlet weak var timeRemainingLbl: UILabel!
var table: Table!
func configureLiveCell(_ NT: Table) {
layer.cornerRadius = 20
tableNumberLbl.text = "T" + String(NT.number)
timeRemainingLbl.text = String(time)
}
func configureCell(_ NT: Table) {
tableNumberLeftLabel.text = "T" + String(NT.number)
guestNumbersLabel.text = "COVERS:" + String(NT.guests)
timeInTableLabel.text = "TIME IN:" + NT.timeIn
}
}
From the viewController that creates the data to reload:
import UIKit
var NT = Table.MyTables.newTable
class NewTableVC: UIViewController {
@IBOutlet weak var tableViewLiveCells: UITableView!
@IBOutlet weak var imageTableCreated: UIImageView!
@IBOutlet weak var tableNumberTF: UITextField!
@IBOutlet weak var guestNumberTF: UITextField!
@IBOutlet weak var timeLabelTable: UILabel!
@IBOutlet weak var prebooked: UISwitch!
@IBOutlet weak var dateLabel: UILabel!
@IBAction func createButton(_ sender: AnyObject) {
print("createButton tapped")
let newNumber = Int(tableNumberTF.text!)
let newGuestNumber = Int(guestNumberTF.text!)
let currentTimeArrival:String? = timeLabelTable.text
if (tableNumberTF.text?.isEmpty)! || (guestNumberTF.text?.isEmpty)! {
let alert = UIAlertController(title: "Missing input(s)", message: "Please fill in all the gaps", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Back Home", style: .default, handler: {(alert: UIAlertAction) in
self.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
} else {
let presentTable = Table(number: newNumber!, guests: newGuestNumber!, timeIn: currentTimeArrival!)
NT.append(presentTable)
print("This is a new table with the following values")
print(presentTable.number)
print(presentTable.guests)
print(presentTable.timeIn)
print(NT.count)
imageTableCreated.alpha = 1
imageTableCreated.layer.zPosition = 500
tableViewLiveCells.reloadData()
let deadline = DispatchTime.now() + .seconds(1)
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}
DispatchQueue.main.asyncAfter(deadline: deadline) {
self.imageTableCreated.alpha = 0
self.imageTableCreated.layer.zPosition = -500
}
}
}
}
reloadData()
doesn't work for any viewController
except for the previous one to this NewTableVC
viewController
that creates the new data, which is the first one I created after creating the other ones.
Could it be that the issue has to do with copying and pasting the same tableViewLiveCells
to all the other view controllers?
If so, I attempted to re-create the table views but it won't let me connect the labels again.
I solved it checking that the tableViewLiveCells
had the delegate
and dataSource
from the respective current viewController
containing it, rather than the main one I used by copying and pasting.