ios - rectForRowAtIndexPath crashing with defined indexPath in UITableView -
the following code crashing @
currentcellrect = tableview.rectforrowatindexpath(indexpaths[0])
but sometimes.
public func showcellscrollcount(animated:bool) { self.tableview.addobserver(self, forkeypath: "contentoffset", options: nskeyvalueobservingoptions.new, context: nil) self.tableview.addobserver(self, forkeypath: "dragging", options: nskeyvalueobservingoptions.new, context: nil) } override public func observevalueforkeypath(keypath: string?, ofobject object: anyobject?, change: [string : anyobject]?, context: unsafemutablepointer<void>) { switch (keypath, object) { case (.some("contentoffset"), _): self.updatescrollposition() default: super.observevalueforkeypath(keypath, ofobject: object, change: change, context: context) } } func updatescrollposition() { let indexpaths = tableview.indexpathsforvisiblerows var currentcellrect:cgrect? if let indexpaths = indexpaths { if indexpaths.count > 0 { currentcellrect = tableview.rectforrowatindexpath(indexpaths[0]) scrollcountview.currentscrollcountnum = indexpaths[0].row } } }
it crashes "bad_access". have idea why?
...
edit: possible it's happening because i'm calling tableview.reloaddata() right before add observers, , call isn't finished yet?
updated
your code works fine since added tableview controller , called showcellscrollcount
func once. call showcellscrollcount
, call multiple times? can problem if so.
update 3
here problem:
public var totalscrollcountnum = 0 { didset { scrollcountview.totalscrollcountnum = totalscrollcountnum showcellscrollcount(false) } }
move showcellscrollcount(false)
initializers
Comments
Post a Comment