ios - How to get cells with different cell classes -
i need text textfield
i.e kgcustomcell , kgrepscustomcell. need fetch data field, when run buttonclicked method.
i have tried add instance variables, contains kg , reps, first time click button, it's empty. second time it's okay. how can load data in correct way?
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let index = indexpath.row if indexpath.row == 0 && indexpath.section == 0 { let exercisename = tableview.dequeuereusablecellwithidentifier("exercise name", forindexpath: indexpath) as! loggedexercisenamecell exercisename.lblexercisename.text = self.exercise?.name return exercisename } if index == 0 && indexpath.section == 1 { let txtfieldkg = tableview.dequeuereusablecellwithidentifier("text kg", forindexpath: indexpath) as! kgcustomcell return txtfieldkg } if index == 1 && indexpath.section == 1 { let txtfieldreps = tableview.dequeuereusablecellwithidentifier("text reps", forindexpath: indexpath) as! kgrepscustomcell //kg = txtfieldreps.textreps.text return txtfieldreps } if index == 2 && indexpath.section == 1 { let btnlog = tableview.dequeuereusablecellwithidentifier("button log", forindexpath: indexpath) as! buttonlogworkoutcustomcell btnlog.btnlogexercise.addtarget(self, action: #selector(addlogviewcontroller.buttonclicked(_:)), forcontrolevents: uicontrolevents.touchupinside) // kg = txtfieldreps.textreps.text return btnlog } if indexpath.section == 2 { let loggedexerciseinformation = tableview.dequeuereusablecellwithidentifier("logged exercise", forindexpath: indexpath) as! loggedexercisecustomcell return loggedexerciseinformation } let nocell = tableview.dequeuereusablecellwithidentifier("button log", forindexpath: indexpath) return nocell } func buttonclicked(sender:uibutton) { let button = sender uibutton if let superview = button.superview { if (superview.superview as? buttonlogworkoutcustomcell) != nil { try! logmanagerdao.sharedinstance.realm.write({ exercise?.loggedkg = 4//int(txtkg.text!)! exercise?.loggedreps = 4//int(txtreps.text!)! log!.addexercisetolog(exercise!) loadloggedexercise() tableview.reloaddata() }) } } }
if want text of textfield can use delegate method of uitextfield this, first declare 2 instance var 2 textfield's value
var strkg: string = "" var strreps: string = ""
now set delegate textfield in cellforrowatindexpath
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let index = indexpath.row if indexpath.row == 0 && indexpath.section == 0 { let exercisename = tableview.dequeuereusablecellwithidentifier("exercise name", forindexpath: indexpath) as! loggedexercisenamecell exercisename.lblexercisename.text = self.exercise?.name return exercisename } if index == 0 && indexpath.section == 1 { let txtfieldkg = tableview.dequeuereusablecellwithidentifier("text kg", forindexpath: indexpath) as! kgcustomcell txtfieldreps.textfield.tag = index txtfieldkg.textfield.delegate = self return txtfieldkg } if index == 1 && indexpath.section == 1 { let txtfieldreps = tableview.dequeuereusablecellwithidentifier("text reps", forindexpath: indexpath) as! kgrepscustomcell txtfieldreps.textfield.tag = index txtfieldreps.textfield.delegate = self return txtfieldreps } if index == 2 && indexpath.section == 1 { let btnlog = tableview.dequeuereusablecellwithidentifier("button log", forindexpath: indexpath) as! buttonlogworkoutcustomcell btnlog.btnlogexercise.addtarget(self, action: #selector(addlogviewcontroller.buttonclicked(_:)), forcontrolevents: uicontrolevents.touchupinside) // kg = txtfieldreps.textreps.text return btnlog } if indexpath.section == 2 { let loggedexerciseinformation = tableview.dequeuereusablecellwithidentifier("logged exercise", forindexpath: indexpath) as! loggedexercisecustomcell return loggedexerciseinformation } let nocell = tableview.dequeuereusablecellwithidentifier("button log", forindexpath: indexpath) return nocell }
now add delegate method of uitextfield
func textfielddidendediting(textfield: uitextfield) { if(textfield.tag == 0) { self.strkg = textfield.text } else { self.strreps = textfield.text } }
now use 2 string object in button action method.
Comments
Post a Comment