ViewController 中的 CustomCell 标签不显示数据

Posted

技术标签:

【中文标题】ViewController 中的 CustomCell 标签不显示数据【英文标题】:CustomCell label in ViewController not displaying data 【发布时间】:2019-08-01 21:35:59 【问题描述】:

下午好,我正在 Firebase 中运行一个查询,我得到的信息我用它来运行另一个查询。我遇到的问题是在第二个 CustomCell 上显示数据。我正在使用打印语句,数据显示在控制台中。 Segues 按预期工作,TableViews 设置为数据源和委托,IBOutlets 到位,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
    

 


视图控制器

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

    

第二个视图控制器

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])

【问题讨论】:

【参考方案1】:

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

prepare for segue 中,您正在设置空的ptdatas 列表。

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

【讨论】:

你是绝对正确的,列表是空的。我刚刚在为 segue 做准备时使用了 print 语句“print(ptdatas),我得到的只是 []。现在,我进行了您建议的更改,但是在更新 PatlistCell 中的委托时出现错误。我正在使用这个: self.delegate?.cellButtonPressed(ptdatas: [PTData]) 并且错误是“无法将 '[PTData].Type' 类型的值转换为预期的参数类型 '[PTData]'。有任何想法吗?谢谢! 已找到解决方案,我已更新初始代码以反映更改。谢谢

以上是关于ViewController 中的 CustomCell 标签不显示数据的主要内容,如果未能解决你的问题,请参考以下文章

需要从另一个viewController调用其他viewController中的方法

关闭以编程方式呈现在嵌套在 NavigationController 中的 ViewController 中的 ViewController 崩溃,但并非每次

在另一个 ViewController 中的 ViewController 之间显示和切换

如何将 ViewController.swift 连接到 Storyboard 中的 ViewController?

从 XIB Viewcontroller 中的 segmentcontrol 到 Storyboard Viewcontroller

检查当前 viewController 类是不是为 IOS 7 中的 Person ViewController 时出现问题