I'm new in Swift. I want to create a simple filling word app using UICollectionView. How can i custom UICollectionViewCell when the answer has 2 words like "Hello World". I want "World" will enter in the next row. And please show me how can center the cell that means "Hello" and "World" are center? Thank for your helping. Here is my code:
var arrayCharacter = ["H","E","L","L","O","W","O","R","L","D"]
extension QuestionVC: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayCharacter.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "quizColCell", for: indexPath) as! QuizCollectionCell
cell.lblCharacter.text = arrayCharacter[indexPath.row]
return cell
}
}
2 ways I can think of :
first way - consider to redesign the DATA from :
var arrayCharacter = ["H","E","L","L","O","W","O","R","L","D"].
to
var arrayWords = [ "HELLO", "WORLD"]
Each collectionViewCell will represent one WORD.
Each cell will be 1 row (full width) In that cell you will create label for each letter (and hide labels you don't use for the word)
The second way , by using SECTIONS (google collectionView sections).
the DATA need to be :
array of array :
sections[0] = ["h","e", "l","l","o"].
sections[1] = ["w","o","r","l","d"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.