ios - 如何自定义collectionView单元格?
问题描述
我是 Swift 的新手。我想使用 UICollectionView 创建一个简单的填充词应用程序。当答案有 2 个单词(如“Hello World”)时,我如何自定义 UICollectionViewCell。我希望“世界”将进入下一行。请告诉我如何将表示“Hello”和“World”的单元格居中?感谢您的帮助。这是我的代码:
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 种方法:
第一种方法- 考虑重新设计 DATA 来自:
var arrayCharacter = ["H","E","L","L","O","W","O"," R","L","D"]。
到
var arrayWords = [ "HELLO", "WORLD"]
每个 collectionViewCell 将代表一个 WORD。
每个单元格将是 1 行(全宽) 在该单元格中,您将为每个字母创建标签(并隐藏您不用于单词的标签)
第二种方式,通过使用 SECTIONS(谷歌 collectionView 部分)。
数据需要是:
数组数组:
sections[0] = ["h","e", "l","l","o"]。
部分[1] = ["w","o","r","l","d"]
推荐阅读
- typescript - 使用组合 api 和 typescript 类型系统强类型化 vue 组件的 props
- python - Visual Studio Code 和 PowerShell 无法识别新的 Python 版本
- angular - 如何在 paperbits 中使用 Angular 组件
- python - 如何在没有输入和输出信息的情况下修复三个测试用例错误?
- jquery - jquery如何使用find()来查找脚本标签内的变量?
- flutter - Flutter (web) video_player 自动播放
- java - 扩展类中的方法方面不起作用
- angular - Error during template compile of 'AppRoutingModule' Function expressions are not supported in decorators
- javascript - 未捕获的类型错误:allClearButton.addEventListener 不是函数
- snowflake-cloud-data-platform - 雪花枢轴属性值到对象数组中的列中