ios - Swift run function before collectionView's functions -
i have got function in swift code
func myfunc(){ let jsondata2 = try jsonserialization.jsonobject(with: data, options: []) as! [[string: any] self.comments = jsondata2 }
and in collectionview's numberofitemsinsection
function have this
return comments.count
what need first run myfunc
, after run other collectionview's functions.i tried putting myfunc()
right @ begginning of viewdidload()
,but didn't worked.
you need define comments
array empty, , load relevant data in viewwillappear
class yourviewcontroller: uiviewcontroller { var comments : [yourobjects] = [] override func viewwillappear(_ animated: bool) { super.viewwillappear(animated) self.myfunc() } func myfunc(){ let jsondata2 = try jsonserialization.jsonobject(with: data, options: []) as! [[string: any] self.comments = jsondata2 dispatchqueue.main.async{ self.collectionview.reloaddata() //reload data after } } }
hope helps
Comments
Post a Comment