ios - Pass data to text field in UITableViewCell from Another UITableView ( Not using Segue) -
i have 2 controllers addcontactcontroller
, homepagecontroller
. want select each row in homepage (which load json data web service). pass data of each row , navigate addcontactcontroller
tableviewcell
. how can ? code little bit messy. suggestions ?
addcontactcontroller class
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let mycell = tableview.dequeuereusablecell(withidentifier: "cellid") as! addcontactcell let note = tableview.dequeuereusablecell(withidentifier: "note") as! note mycell.input_field.delegate = self note.input_field.delegate = self as? uitextviewdelegate if indexpath.section == 0 { mycell.namelabel.text = items[indexpath.row] if (mycell.namelabel.text == "occupation") { mycell.input_field.attributedplaceholder = nsattributedstring(string: "occupation", attributes: [nsforegroundcolorattributename:uicolor(red:0.81, green:0.81, blue:0.83, alpha:1.0)]) } else if (mycell.namelabel.text == "first name"){ mycell.input_field.attributedplaceholder = nsattributedstring(string: "first name", attributes: [nsforegroundcolorattributename:uicolor(red:0.81, green:0.81, blue:0.83, alpha:1.0)]) } else { mycell.input_field.attributedplaceholder = nsattributedstring(string: "last name", attributes: [nsforegroundcolorattributename:uicolor(red:0.81, green:0.81, blue:0.83, alpha:1.0)]) } mycell.mycontactcontroller = self return mycell } else if indexpath.section == 1 { mycell.namelabel.text = "place" mycell.input_field.attributedplaceholder = nsattributedstring(string: "place", attributes: [nsforegroundcolorattributename:uicolor(red:0.81, green:0.81, blue:0.83, alpha:1.0)]) return mycell } else { return note } }
homepagecontroller class
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let mycell = tableview.dequeuereusablecell(withidentifier: "cellid", for: indexpath indexpath) as! recordcell mycell.playbutton.tag = indexpath.row mycell.playbutton.addtarget(self, action: #selector(playaudio(sender:)), for: .touchupinside) mycell.namelabel.text = namearray[indexpath.row] let fullnamearray = namearray[indexpath.row].components(separatedby: " ") let fname = fullnamearray[0].characters.first let lname = fullnamearray[1].characters.first var initials: string = "" if (fname != nil && lname != nil ) { initials.append(fname!) initials.append(lname!) } mycell.shortname.text = initials mycell.mytableviewcontroller = self return mycell } override func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { let storyboard = uistoryboard(name: "instancestoryboard", bundle: nil) let viewcontroller = storyboard.instantiateviewcontroller(withidentifier: "viewcontrolleridentifer") as! addcontactcontroller viewcontroller. self.navigationcontroller?.pushviewcontroller(viewcontroller, animated: true) }
supposed needed pass data store in this
var namearray = [string]()
to cell @ selected row using indexpath tableview.cellforrow(at: indexpath)
samplecode:
homeviewcintroller
override func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { let cell = tableview.cellforrow(at: indexpath) as! recordcell let storyboard = uistoryboard(name: "instancestoryboard", bundle: nil) let viewcontroller = storyboard.instantiateviewcontroller(withidentifier: "viewcontrolleridentifer") as! addcontactcontroller viewcontroller.obj = cell.namelabel.text self.navigationcontroller?.pushviewcontroller(viewcontroller, animated: true) }
addcontactviewcontroller
func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell") cell.mytxt.text = obj return cell }
Comments
Post a Comment