ISSUE HAS BEEN FIXED. SEE POST CustomCell label in ViewController not displaying data
I have a UITableViewCell with an IBAction button that calls a query to Firestore. Once the Query runs i want to segue to another UITableViewCell and show the data. I'm not sure what i'm missing or doing wrong here. Please assist. Thank you.
Cell Delegate
protocol MyCellDelegate {
func sendDataFromCell(_ sender: Any)
}
PTLISTCELL
class PtlistCell: UITableViewCell {
@IBOutlet private weak var ptnameLbl: UILabel!
@IBOutlet private weak var assignedmdLbl: UILabel!
@IBOutlet private weak var officemdLbl: UILabel!
@IBOutlet private weak var roomnumberLbl: UILabel!
@IBOutlet private weak var seenoseeLbl: UILabel!
@IBOutlet private weak var assigneddateLbl: UILabel!
@IBOutlet weak var ptdataBtn: UIButton!
var ptdatainfoRef: CollectionReference!
var query: Query!
var patdata = [PTData]()
var delegate: MyCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
ptdatainfoRef = Firestore.firestore().collection(PTLIST_REF)
}
func configureCell(ptlist: PTList) {
ptnameLbl.text = ptlist.ptname
assignedmdLbl.text = ptlist.assignedmd
officemdLbl.text = ptlist.officemd
roomnumberLbl.text = ptlist.room
seenoseeLbl.text = ptlist.seenosee
assigneddateLbl.text = ptlist.assigneddate
}
@IBAction func ptdataTapped(_ sender: Any) {
let query = ptdatainfoRef.whereField("ptname", isEqualTo:
(ptnameLbl.text!))
.whereField("assignedmd", isEqualTo: (assignedmdLbl.text!))
.whereField("officemd", isEqualTo: (officemdLbl.text!))
.whereField("room", isEqualTo: (roomnumberLbl.text!))
.whereField("seenosee", isEqualTo: (seenoseeLbl.text!))
.whereField("assigneddate", isEqualTo:
(assigneddateLbl.text!))
query.getDocuments { (snapshot, error) in
if let err = error {
debugPrint("error getting data: \(err)")
} else {
guard let snap = snapshot else { return }
for document in snap.documents {
let data = document.data()
let ptname = data[PTNAME] as? String ?? ""
let dob = data[DOB] as? String ?? ""
let fin = data[FIN] as? String ?? ""
let officemd = data[OFFICEMD] as? String ?? ""
let assignedmd = data[ASSIGNEDMD] as? String ?? ""
let assigneddate = data[ASSIGNEDDATE] as? String ?? ""
let room = data[ROOM] as? String ?? ""
let diagnoses = data[DIAGNOSES] as? String ?? ""
let reasonforadmitorconsult =
data[REASONFORADMITORCONSULT] as? String ?? ""
let goalofhospitalization =
data[GOALOFHOSPITALIZATION] as? String ?? ""
let seenosee = data[SEENOSEE] as? String ?? ""
let notestocbo = data[NOTESTOCBO] as? String ?? ""
let comments = data[COMMENTS] as? String ?? ""
let hosp = data[HOSP] as? String ?? ""
let team = data[TEAM] as? String ?? ""
let newPtdata = PTData (ptname: ptname, dob: dob, fin:
fin, officemd: officemd, assignedmd: assignedmd, assigneddate:
assigneddate, room: room, diagnoses: diagnoses,
reasonforadmitorconsult: reasonforadmitorconsult,
goalofhospitalization: goalofhospitalization, seenosee: seenosee,
notestocbo: notestocbo, comments: comments, hosp: hosp, team: team)
self.patdata.append(newPtdata)
print(document.data())
};print(self.delegate)
if(self.delegate != nil) {
self.delegate?.sendDataFromCell(sender)
}
}
}
}
}
ResultsdataVC
class ResultsdataVC: UIViewController, UITableViewDataSource,
UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var ptlist: PTList!
var ptlists = [PTList]()
var tableVc: PatdataVC!
var ptdata: PTData!
var patdata = [PTData]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return ptlists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier:
"PtlistCell", for: indexPath) as? PtlistCell {
cell.configureCell(ptlist: ptlists[indexPath.row])
return cell
}
return UITableViewCell()
}
}
extension ResultsdataVC: MyCellDelegate {
func sendDataFromCell(_ sender: Any) {
self.performSegue(withIdentifier: "gotoptdata", sender: Any?.self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "gotoptdata" {
let vc = segue.destination as! PatdataVC
vc.patdata = patdata
}
}
}
patdataVC
class PatdataVC: UIViewController, UITableViewDelegate,
UITableViewDataSource {
var ptdata: PTData!
var patdata = [PTData]()
var tableViewCellDelegate: MyCellDelegate?
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return patdata.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier:
"PtdataCell", for: indexPath) as? PtdataCell {
cell.configureCell(patdata: patdata[indexPath.row])
return cell
}
return UITableViewCell()
}
}
PATDATACELL
class patdataCell: UITableViewCell {
@IBOutlet weak var ptnameLbl: UILabel!
@IBOutlet weak var dobLbl: UILabel!
@IBOutlet weak var finLbl: UILabel!
@IBOutlet weak var officemdLbl: UILabel!
@IBOutlet weak var assignedmdLbl: UILabel!
@IBOutlet weak var assigneddateLbl: UILabel!
@IBOutlet weak var roomLbl: UILabel!
@IBOutlet weak var diagnosesLbl: UILabel!
@IBOutlet weak var reasonforadmitorconsultLbl: UILabel!
@IBOutlet weak var seenoseeLbl: UILabel!
@IBOutlet weak var goalofhospLbl: UILabel!
@IBOutlet weak var notestocboLbl: UILabel!
@IBOutlet weak var commentsLbl: UILabel!
var ptdata: PTData!
override func awakeFromNib() {
super.awakeFromNib()
}
func configureCell(ptdata: PTData) {
ptnameLbl.text = ptdata.ptname
dobLbl.text = ptdata.dob
finLbl.text = ptdata.fin
officemdLbl.text = ptdata.officemd
officemdLbl.text = ptdata.officemd
assignedmdLbl.text = ptdata.assignedmd
roomLbl.text = ptdata.room
diagnosesLbl.text = ptdata.diagnoses
reasonforadmitorconsultLbl.text = ptdata.reasonforadmitorconsult
seenoseeLbl.text = ptdata.seenosee
goalofhospLbl.text = ptdata.goalofhosp
notestocboLbl.text = ptdata.notestocbo
commentsLbl.text = ptdata.comments
}
}
A segue can only be triggered from a vc instance not a tableCell instance
and for that this should exist inside the vc
if you need to trigger the segue from the cell button action then you need to create a delegate
and assign it inside the vc's
cellForRowAt
Now inside the cell you can do
a separate tip here you send
self
insender
which is the cell itselfso you can't cast it to button here