ios - Retrieve textfield.text which is inside custom cell -


i have tableview has custom cells. each cell has 3 textfields: dayinweek, starttime, endtime. in below image, has 2 rows. user can click + button add more rows.

if user click submit button, want loop every rows, collect 3 textfields data, , store in array or whatever.

enter image description here

custom tableviewcell:

import uikit  class regularschedulecell: uitableviewcell {       @iboutlet weak var dayinweek: uitextfield!     @iboutlet weak var starttime: uitextfield!     @iboutlet weak var endtime: uitextfield!      override func awakefromnib() {         super.awakefromnib()         // initialization code       } } 

and view controller:

import uikit   class regularschedulevc: uiviewcontroller, uitableviewdelegate,uitableviewdatasource {      @iboutlet weak var tableview: uitableview!       var numofrow = 1      override func viewdidload() {         super.viewdidload()         tableview.datasource = self         tableview.delegate = self     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }     func numberofsections(in tableview: uitableview) -> int {         return 1     }      func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {         return numofrow     }      func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {         let cellidentifier = "regularschedulecell"         let cell = tableview.dequeuereusablecell(withidentifier: cellidentifier, for: indexpath) as! regularschedulecell          return cell         }      func tableview(_ tableview: uitableview, commit editingstyle: uitableviewcelleditingstyle, forrowat indexpath: indexpath) {         if (editingstyle == uitableviewcelleditingstyle.delete) {             numofrow -= 1             self.tableview.deleterows(at: [indexpath], with: uitableviewrowanimation.right)          tableview.reloaddata()         }     }       func insertnewrow(_ sender: uibarbuttonitem) {         if numofrow < 7 {             numofrow += 1             tableview.reloaddata()          }        }     override func setediting(_ editing: bool, animated: bool) {         super.setediting(editing, animated: animated)         tableview.setediting(editing, animated: animated)     }   } 

at moment, try use uitextfielddelegate

func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {         let cellidentifier = "regularschedulecell"         let cell = tableview.dequeuereusablecell(withidentifier: cellidentifier, for: indexpath) as! regularschedulecell          cell.dayinweek.delegate = self         cell.starttime.delegate = self         cell.endtime.delegate = self         return cell 

and

func textfielddidendediting(_ textfield: uitextfield) {         allcellstext.append(textfield.text!) //allcellstext array         print(allcellstext)      } 

so when user finish editing, add data array. however, not satisfy requirement, because:

  • on same cell: can not know if data belong dayofweek, or starttime, or endtime

  • on 2 different cells: can not know if data belong to, let say, dayofweek of 1st cell or dayofweek of 2nd cell.

therefore, how can loop cells, 3 text fields data? thanks

method 1:

make array of key pairs as-

    var arrayofkeypairs = [[string:any]]()      arrayofkeypairs.append(["header":"xx",                             "value" : "",                             “id”: "dsd",                             "order" : 0]) 

we replacing default values user input values as-

func textfielddidendediting(_ textfield: uitextfield) {         let center: cgpoint = textfield.center         let rootviewpoint: cgpoint = textfield.superview!.convert(center, to: tableview)         let indexpath: indexpath = tableview.indexpathforrow(at: rootviewpoint)! indexpath         arrayofkeypairs[indexpath.row ]["value"] = textfield.text//here appending(replacing) data array } 

on click of submit button, cross check received as-

func tapsonnext(){   self.view.endediting(true)//for adding last text field value dismiss keyboard   print(arrayofkeypairs) } 

method 2: can cell data accessing cell particular indexpath as

func tapsonnext(){      let indexpath = indexpath(row: 0, section: 0)      let cell = tableview.cellforrow(at: indexpath) as! customtableviewcell      print(cell.mytextfield.text)   } 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

angular - Copying node modules to wwwroot AspNetCore -