简体   繁体   English

如何自定义collectionView单元格?

[英]How can custom collectionView cell?

I'm new in Swift. 我是Swift的新手。 I want to create a simple filling word app using UICollectionView. 我想使用UICollectionView创建一个简单的填充词应用程序。 How can i custom UICollectionViewCell when the answer has 2 words like "Hello World". 当答案中有2个单词(例如“ Hello World”)时,如何自定义UICollectionViewCell。 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 : 我可以想到2种方式:
first way - consider to redesign the DATA from : 第一种方法 -考虑从以下位置重新设计数据:
var arrayCharacter = ["H","E","L","L","O","W","O","R","L","D"]. var arrayCharacter = [“ H”,“ E”,“ L”,“ L”,“ O”,“ W”,“ O”,“ R”,“ L”,“ D”]。
to
var arrayWords = [ "HELLO", "WORLD"] var arrayWords = [“ HELLO”,“ WORLD”]

Each collectionViewCell will represent one WORD. 每个collectionViewCell将代表一个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) 每个单元格将为1行(全角)。在该单元格中,您将为每个字母创建标签(并隐藏该单词不使用的标签)

The second way , by using SECTIONS (google collectionView sections). 第二种方式 ,通过使用SECTIONS(Google collectionView部分)。
the DATA need to be : 数据必须是:
array of array : 数组数组:
sections[0] = ["h","e", "l","l","o"]. 区段[0] = [“ h”,“ e”,“ l”,“ l”,“ o”]。
sections[1] = ["w","o","r","l","d"] 区段[1] = [“ w”,“ o”,“ r”,“ l”,“ d”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM