本文介绍了不能运行到另一个UITableViewCell的UITableViewCell segue-委托为NIL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已解决.查看帖子 ViewController中的CustomCell标签未显示数据

我有一个带有IBAction按钮的UITableViewCell,该按钮调用了查询到Firestore.查询运行后,我想选择另一个并显示UITableViewCell的数据.我不确定我想念的是什么或在这里做错了.请协助.谢谢你.

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.

单元代表

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

patdataCell类:UITableViewCell {

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

}

}

推荐答案

只能从vc实例而不是tableCell实例触发segue

A segue can only be triggered from a vc instance not a tableCell instance

 self.performSegue(withIdentifier: "gotoptdata",sender: self)

为此,它应该存在于vc中

and for that this should exist inside the vc

 func prepare(for segue: UIStoryboardSegue, sender: AnyObject!) {
            if segue.identifier == "gotoptdata" {
                let button = sender as! UIButton
                let view = button.superview!
                let cell = view.superview as! patdataCell
                let indexPath = PTData.ptdata()

            }
}

如果您需要通过单元格按钮操作触发触发,则需要创建一个委托

if you need to trigger the segue from the cell button action then you need to create a delegate

class ptlistCell: UITableViewCell {
   weak var delegate:VCName?
   ....
} 

并将其分配到vc的 cellForRowAt

let cell = ///
cell.delegate = self

现在您可以在单元格内

delegate?.performSegue...............


这里有一个单独的提示,您将在单元格本身的 sender 中发送 self

self.performSegue(withIdentifier: "gotoptdata",sender: self)

所以您不能将其投射到此处的按钮

so you can't cast it to button here

let button = sender as! UIButton

这篇关于不能运行到另一个UITableViewCell的UITableViewCell segue-委托为NIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:22