swift - How to keep value updated after button is pressed? -


i have 2 view controllers, both table views.

depending on row select in first table view, second table view cell's filled subcategories of min category select in initial table view. in second table view there button.

i trying pass value of row selected in first table view button in table view cell of second table view in order use in future , keep track of row selected in first table view.

what best way implement this?

every time click button in table view cell of second table view print(initialrow) it's 0, how can change shows correct value index of row selected in first table view?

here code first table view:

import uikit  var trainingdict = ["ball handling" : ["1 ball stationary drills", "1 ball combo moves", "2 ball stationary drills", "2 ball combo moves", "2 ball partner drills", "handle hoop drills", "placeholder"], "shooting" : ["form shooting", "spot shooting", "off dribble shots", "pull jumpshots", "catch & shoots", "free throws", "partner shooting"], "defense" : ["5 star drill", "full court def. slides", "1 v 1 closeouts", "gauntlet drill", "tennis ball reaction drill", "lane slides", "place holder"], "advanced drills" : ["d man series", "iso series", "double move series", "gauntlet series", "john wall drill", "floater series", "placeholder"], "vertimax drills" : ["one foot jumps", "box jumps", "resitance slides", "resistance jumps", "resistance ball handling", "vertimax sprints", "slam drill"], "full workouts" : ["workout a", "workout b", "workout c", "workout d", "workout e", "workout f", "workout g"], "bhb products" : ["handle hoops", "handle cubes", "strech bands", "advocare", "placeholder", "placeholder2", "placeholder3"]]  //gradient creation var gradient : cagradientlayer! var drilllabelgradient : cagradientlayer! var myindex = 0   class viewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource {      @iboutlet weak var tableview: tableview!      var trainingcategories = [string]()     var arrayforkey = array(trainingdict.values)     var selectedkey = 0     var rowicon = [#imageliteral(resourcename: "bballicon"), #imageliteral(resourcename: "pullup"), #imageliteral(resourcename: "kidbb"), #imageliteral(resourcename: "girlbb"), #imageliteral(resourcename: "3ball"), #imageliteral(resourcename: "dstance"), #imageliteral(resourcename: "dunk")]      public func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int     {         return rowicon.count     }      public func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell     {         let cell = tableview.dequeuereusablecell(withidentifier: "bell") as! viewcontrollertableviewcell          //cell details         cell.backgroundcolor = uicolor.clear          //gradient details         gradient = cagradientlayer()         gradient.frame = tableview.bounds         gradient.colors = [uicolor.black.cgcolor, uicolor.darkgray.cgcolor, uicolor.black.cgcolor]         tableview.layer.insertsublayer(gradient, at: 0)         gradient.startpoint = cgpoint(x: 0.0, y: 0.0)         gradient.endpoint = cgpoint(x: 1.0, y: 1.0)          //details text label of cell displays         var trainingcategories = array(trainingdict.keys)          myindex = indexpath.row          cell.textlabel?.font = uifont(name: "symbol", size: 24.0)         cell.textlabel?.text = trainingcategories[myindex]         cell.textlabel?.textcolor = uicolor.white         cell.bordercolor = uicolor.white         cell.borderwidth = 1.5         cell.imageview?.image = rowicon[myindex]          print(trainingcategories.count)          return cell     }      func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) {         print(indexpath.row)         selectedkey = indexpath.row         performsegue(withidentifier: "segue", sender: self)     }      override func prepare(for segue: uistoryboardsegue, sender: any?) {         if segue.identifier == "segue" {             if let secondtableview = segue.destination as? drillsviewcontroller {                 //how labels in second table view corresspond first table view labels                 secondtableview.keyindex = selectedkey                 secondtableview.arrayforkey2 = arrayforkey                 secondtableview.initialrow = selectedkey             }         }     }           override func viewdidload() {         super.viewdidload()         tableview.delegate = self         tableview.datasource = self         trainingcategories = array(trainingdict.keys)     } } 

code second table view:

import uikit import avkit import avfoundation  class drillsviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource {      var initialrow = int()     var arrayforkey2 = [[string]]()     var keyindex = int()     var headlabel = string()     var labels = array(trainingdict.keys)      @iboutlet weak var tableview: drillstableview!      @iboutlet weak var drilllabel: uilabel!      @iboutlet weak var labelbackground: uiview!      @ibaction func back(_ sender: any) {         performsegue(withidentifier: "back", sender: self)     }      public func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int     {         return arrayforkey2.count     }      func numberofsections(in tableview: uitableview) -> int {         return 1     }      public func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell     {         let cell = tableview.dequeuereusablecell(withidentifier: "cell") as! drillstableviewcell         cell.playbutton.tag = indexpath.row          //clear background color needed in order display gradient cell         cell.backgroundcolor = uicolor.clear            //details cell label display         cell.borderwidth = 1.5         cell.bordercolor = uicolor.white         cell.drilltitle.text = "\(arrayforkey2[keyindex][indexpath.row])"         cell.drilltitle.font = uifont(name: "symbol", size: 18.0)         cell.drilltitle.textcolor = uicolor.white          return cell     }      func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) {         let cell = tableview.dequeuereusablecell(withidentifier: "cell") as! drillstableviewcell         cell.initialrow = initialrow         print(cell.initialrow)         print(indexpath.row)     }      override func viewdidload() {         super.viewdidload()         tableview.delegate = self         tableview.datasource = self         drilllabel.text = labels[keyindex]     } } 

code second table view's table view cell:

import uikit import avfoundation import avkit  class videoplayerview: uiview {      let pausebutton: uibutton = {         let button = uibutton(type: .system)         button.setimage(#imageliteral(resourcename: "triangle 2"), for: .normal)         button.translatesautoresizingmaskintoconstraints = false         button.tintcolor = uicolor.white         button.ishidden = false         button.addtarget(self, action: #selector(handlepause), for: .touchupinside)          return button     }()      var isplaying = false      func handlepause() {         if isplaying {             player?.pause()             pausebutton.alpha = 1.0         } else {              player?.play()             pausebutton.alpha = 0.01         }          isplaying = !isplaying     }      //container view holds sublayers video control objects     let controlscontainerview: uiview = {         let view = uiview()         view.backgroundcolor = uicolor(white: 0, alpha: 1.0)         return view     }()      override init(frame: cgrect) {         super.init(frame: frame)          //configures container view (video's background)         controlscontainerview.frame = frame         addsubview(controlscontainerview)         backgroundcolor = uicolor.black          //following adds pause/play button video         controlscontainerview.addsubview(pausebutton)         pausebutton.centerxanchor.constraint(equalto: centerxanchor).isactive = true         pausebutton.centeryanchor.constraint(equalto: centeryanchor).isactive = true         }      var player: avplayer?      //function sets video playback     func setupplayerview(for url: url) {         player = avplayer(url: url)          //video renders if specify 'playerlayer'         let playerlayer = avplayerlayer(player: player)         self.layer.insertsublayer(playerlayer, at: 1)         playerlayer.frame = frame          player?.play()          //attached obeserver of 'player' tell when 'player' ready         player?.addobserver(self, forkeypath: "currentitem.loadedtimeranges", options: .new, context: nil)     }      //method called every time add obserever object     override func observevalue(forkeypath keypath: string?, of object: any?, change: [nskeyvaluechangekey : any]?, context: unsafemutablerawpointer?) {         //strring lets avplayer know ready         if keypath == "currentitem.loadedtimeranges" {                     //configures container view while video playing             controlscontainerview.backgroundcolor = uicolor.clear             pausebutton.alpha = 0.03             isplaying = true                 }     }      required init?(coder adecoder: nscoder) {         fatalerror("init(coder:) has not been implemented")     }     }   class drillstableviewcell: uitableviewcell {      var videourl:[url] = [url(fileurlwithpath: "/users/jordanlagrone/desktop/blackheartbb/blackheartbb/dunk.mov"), url(fileurlwithpath: "/users/jordanlagrone/desktop/blackheartbb/blackheartbb/mk.mov")]     var video = url(fileurlwithpath: string())     var initialrow = int()      var player: avplayer?     var playercontroller = avplayerviewcontroller()      @iboutlet weak var drilltitle: uilabel!      @iboutlet weak var playbutton: uibutton!     @ibaction func watchbutton(_ sender: uibutton) {         print(initialrow)         print(string(sender.tag))          //controls video background view         if let keywindow = uiapplication.shared.keywindow {             let view = uiview(frame: keywindow.frame)             view.backgroundcolor = uicolor.white             let singlevideourl = videourl[sender.tag]              view.frame = cgrect(x: 0.0, y: 0.0, width: keywindow.frame.width, height: keywindow.frame.height)              let videoplayerframe = cgrect(x: 0, y: 0, width: keywindow.frame.width, height: keywindow.frame.width * 9 / 16)             let videoplayerview = videoplayerview(frame: videoplayerframe)             videoplayerview.setupplayerview(for: singlevideourl)             view.addsubview(videoplayerview)              keywindow.addsubview(view)             uiview.animate(withduration: 0.2, delay: 0, options: .curveeaseout, animations: {                 view.frame = keywindow.frame             }, completion: { (completedanimation) in                 // possible features implemented later                 uiapplication.shared.isstatusbarhidden = true             })                     }     } } 

in cellforrow method in drillsviewcontroller, dequeue cell drillstableviewcell never set initialrow attribute of drillstableviewcell. means when cell dequeued, line sets value of initialrow in drillstableviewcell is

var initialrow = int() 

what need assign value of initialrow drillsviewcontroller initialrow drillstableviewcell in cellforrow method

public func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "cell") as! drillstableviewcell     cell.playbutton.tag = indexpath.row      //clear background color needed in order display gradient cell     cell.backgroundcolor = uicolor.clear      cell.initialrow = initialrow //this line missing      //details cell label display     cell.borderwidth = 1.5     cell.bordercolor = uicolor.white     cell.drilltitle.text = "\(arrayforkey2[keyindex][indexpath.row])"     cell.drilltitle.font = uifont(name: "symbol", size: 18.0)     cell.drilltitle.textcolor = uicolor.white      return cell } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -