2017-09-11 660 views
0

데이터 소스로 문자열 배열이 포함 된 빈 UICollectionView가 있습니다.첫 번째 항목을 추가 한 후 UICollectionView.reloadData()가 작동 한 다음 뒤가 하나만 실행되는 것으로 보입니다.

배열에 첫 번째 문자열을 추가하고 collectionView에서 reloadData()를 호출하면 예상대로 작동합니다. 두 번째 문자열을 추가하고 reloadData()를 호출하면 아무 일도 일어나지 않지만 배열은 확실히 늘어났습니다. 세 번째 항목을 추가하고 reloadData()를 호출하면 두 번째 항목이 나타나지만 세 번째 항목은 나타나지 않습니다. 새로 추가 할 때마다 이전 문자열이 나타납니다.

그런 다음 문자열을 추가하지 않고 reloadData()를 호출하면 마지막 항목이 나타납니다.

컬렉션에 추가 내 코드 :

func addKeyword(keyword: String) { 

    keywords.append(keyword) 
    newKeywordText.text = "" 
    keywordCollection.reloadData() 

} 

그리고 내 collectionView 관련 코드 :

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    print("keywords: at 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 
} 

내가 numberOfItemsInSection 정확한 수를 반환하는 것을 알고, 그래서 난 아무 생각이 왜 그 행동하는 이 방법. 어떤 아이디어?

답변

0

시도 먼저 배열의 인구를 완료하고, 그것은 keywordCollection.reloadData()

당신은 또한 당신은 어떻게 addKeyword 방법을 요구하고 공유 할 수있는 전화 후 완료되면