gpt4 book ai didi

swift - ViewController 中的 CustomCell 标签不显示数据

转载 作者:行者123 更新时间:2023-11-30 10:38:42 26 4
gpt4 key购买 nike

下午好,我正在 Firebase 中运行一个查询,我用得到的信息来运行另一个查询。我遇到的问题是在第二个 CustomCell 上显示数据。我正在使用 print 语句,数据显示在控制台中。 Segues 按预期工作,TableViews 设置为数据源和委托(delegate),IBOutlet 就位, Storyboard中的单元标识符也就位。我找不到解决方案,我不确定我错过了什么。任何帮助是极大的赞赏。

包含第二个查询的单元格

class PatlistCell: UITableViewCell {

@IBOutlet weak var ptnameLbl: UILabel!
@IBOutlet weak var assignedmdLbl: UILabel!
@IBOutlet weak var officemdLbl: UILabel!
@IBOutlet weak var roomnumberLbl: UILabel!
@IBOutlet weak var seenoseeLbl: UILabel!
@IBOutlet weak var assigneddateLbl: UILabel!
@IBOutlet weak var ptdataBtn: UIButton!

var ptdatas = [PTData]()
var ptdatainfoRef: CollectionReference!
var delegate: MyCellDelegate?
var patDataQuery: Query!

override func awakeFromNib() {
super.awakeFromNib()

ptdatainfoRef = Firestore.firestore().collection(PTLIST_REF)
}

func configurePatListCell(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 patDataQuery = 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!))

patDataQuery.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.ptdatas.append(newPtdata)
print(document.data())

}
}
self.delegate?.cellButtonPressed(self.ptdatas) //argument to pass the variable to the VC
}

}

}

View Controller

class ResultsdataVC: UIViewController  {

@IBOutlet weak var resultsTableView: UITableView!

var ptlists = [PTList]()
var ptdatas = [PTData]()

override func viewDidLoad() {
super.viewDidLoad()
resultsTableView.delegate = self
resultsTableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "gotoptinfo" {
let destVC = segue.destination as! PatdataVC
destVC.ptdatas = ptdatas
}
}
}

extension ResultsdataVC: UITableViewDataSource,
UITableViewDelegate, MyCellDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return ptlists.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
if tableView == self.resultsTableView {
let cell = tableView.dequeueReusableCell(withIdentifier:
"PatlistCell", for: indexPath) as? PatlistCell
cell!.configurePatListCell(ptlist: ptlists[indexPath.row])
cell!.delegate = self
return cell!
}
return UITableViewCell()
}

func cellButtonPressed() {
self.ptdatas = ptdatas . //this allows the VC populate the local variable
performSegue(withIdentifier: "gotoptinfo", sender: self)
}
}

第二个单元格

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 roomnumberLbl: UILabel!
@IBOutlet weak var diagnosesLbl: UILabel!
@IBOutlet weak var reasonforadmitorconsultLbl: UILabel!
@IBOutlet weak var goalofhospitalizationLbl: UILabel!
@IBOutlet weak var seenoseeLbl: UILabel!
@IBOutlet weak var notestocboLbl: UILabel!
@IBOutlet weak var commentsLbl: UILabel!
@IBOutlet weak var hospitalLbl: UILabel!
@IBOutlet weak var teamLbl: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
}

func configurePatDataCell(ptdata: PTData) {

ptnameLbl.text = ptdata.ptname
dobLbl.text = ptdata.dob
finLbl.text = ptdata.fin
officemdLbl.text = ptdata.officemd
assignedmdLbl.text = ptdata.assignedmd
assigneddateLbl.text = ptdata.assigneddate
roomnumberLbl.text = ptdata.room
diagnosesLbl.text = ptdata.diagnoses
reasonforadmitorconsultLbl.text =
ptdata.reasonforadmitorconsult
goalofhospitalizationLbl.text = ptdata.goalofhospitalization
seenoseeLbl.text = ptdata.seenosee
notestocboLbl.text = ptdata.notestocbo
commentsLbl.text = ptdata.comments
hospitalLbl.text = ptdata.hosp
teamLbl.text = ptdata.team

}
}

第二个ViewController

class PatdataVC: UIViewController, UITableViewDelegate, 
UITableViewDataSource {

var ptdatas = [PTData]()

@IBOutlet weak var patDataTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

patDataTableView.delegate = self
patDataTableView.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return ptdatas.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
if tableView == patDataTableView {
let cell = tableView.dequeueReusableCell(withIdentifier:
"PatdataCell", for: indexPath) as? PatdataCell
cell!.configurePatDataCell(ptdata: ptdatas[indexPath.row])
return cell!
}
return UITableViewCell()
}
}

单元代表

protocol MyCellDelegate {
func cellButtonPressed (_ ptdatas: [PTData])
}

最佳答案

ptdatas 存在于 ResultsdataVC 和 PatlistCell 中。在您的 PatlistCell 中,会附加此数据,但 ResultsdataVC 中的变量不会更新。

准备segue中,您正在设置空的ptdatas列表。

解决方案是让您的委托(delegate)符合 cellButtonPressed(ptdatas: [PTData]) 并将生成的数组从 tableViewCell 传递到 ResultsdataVC,并在执行 segue 之前更新 ResultsdataVC PTData 数组。

关于swift - ViewController 中的 CustomCell 标签不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57317277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com