ios - UICollectionView.reloadData() works after adding first item, then seems to run one behind -


i have empty uicollectionview array of strings datasource.

when add first string array , call reloaddata() on collectionview works expect. when add second string , call reloaddata() nothing happens, array has grown. when add third item , call reloaddata(), second item appears not third. each new addition results in previous string appearing.

if call reloaddata() without adding string, last item appears.

my code adding collection:

func addkeyword(keyword: string) {      keywords.append(keyword)     newkeywordtext.text = ""     keywordcollection.reloaddata()  } 

and collectionview relevant code:

func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int {      print("keywords: @ refresh \(keywords.count)")     return keywords.count }   func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {      let keyword = keywords[indexpath.row]     let cell = collectionview.dequeuereusablecell(withreuseidentifier: "keywordcloudcell", for: indexpath) as! keywordcloudcell     let keywordlabel:uilabel = (cell.viewwithtag(1) as? uilabel)!     keywordlabel.text = keyword      return cell } 

i know numberofitemsinsection returning correct count, have no idea why behaving way. ideas?

try complete array population first, , when completed call keywordcollection.reloaddata()

can share on how calling addkeyword method


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -