ios - How to load data in UITableView cell from structure -
i trying load data structure table view cell, created custom cell 3 label in it. have 3 text field in view controller , add button want when fill these 3 text field , press add store these 3 values in structure , reload data of table view. structure in other file.
here code structure in datamaster.swift
struct jobdata { var companyname:array<string> = ["ram"] var job:array<string> = ["shyam"] var desc:array<string> = ["dfdf"] }
code addbutton function
@ibaction func addbuttontapped(sender: anyobject) { var company = txtcompname.text var job = txtjob.text var description = txtdesc.text data.companyname.append(company) data.desc.append(description) data.job.append(job) self.jobtableview.reloaddata() print(data.companyname) txtcompname.resignfirstresponder() txtjob.resignfirstresponder() txtdesc.resignfirstresponder() }
the problem in code
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath:indexpath) jobtableviewcell cell.complabel.text = data.companyname[indexpath.row] cell.joblabel.text = data.job[indexpath.row] cell.desclabel.text = data.desc[indexpath.row] return cell }
when reaches code load data in table crashes thread 1:exc_breakpoint(code=exc_i386_bpt,subcode=0x0
)
here below code.
struct jobdata { var companyname:string! var job:string! var desc:string! }
take array var datas = [jobdata]()
now in action method
@ibaction func addbuttontapped(sender: anyobject) { var company = txtcompname.text var job = txtjob.text var description = txtdesc.text let dataobject = jobdata(company: company, job: job, desc: description) datas.append(dataobject) self.jobtableview.reloaddata() txtcompname.resignfirstresponder() txtjob.resignfirstresponder() txtdesc.resignfirstresponder() } in cellforrowatindex method func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath:indexpath) as! jobtableviewcell let data = datas[indexpath.row] if let companyname = data.companyname { cell.complabel.text = companyname } if let job = data.job { cell.joblabel.text = job } if let descr = data.desc { cell.desclabel.text = descr } return cell }
in numberofrowsinsection method return datas.count
check why data.companyname empty , make sure text field have text.
Comments
Post a Comment