Ios 如何自定义UICollectionView单元格项行为?
我正在学习swift和iOS dev 我决定用UICollectionView构建一个小应用程序 所以我的问题是如何定制UICollectionView单元格的行为 以下是我试图实现的目标: 我有两个数组:Ios 如何自定义UICollectionView单元格项行为?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在学习swift和iOS dev 我决定用UICollectionView构建一个小应用程序 所以我的问题是如何定制UICollectionView单元格的行为 以下是我试图实现的目标: 我有两个数组: "temps": [ "Présent", "Passé composé", "Imparfait", "Plus-que-parfait", "Passé simple", "Passé antérieur", "Futur",
"temps": [
"Présent",
"Passé composé",
"Imparfait",
"Plus-que-parfait",
"Passé simple",
"Passé antérieur",
"Futur",
"Futur antérieur",
"Futur proche",
"Présent",
"Passé",
"Passé - forme alternative",
"Présent",
"Imparfait",
"Plus-que-parfait",
"Passé",
"Présent",
"Présent",
"Passé"
],
和“列表”:[
“je suis”,
“是的,
“我最爱你”,
“理智的躯体”,
“你好”,
“我的儿子”,
“j'aiété”,
“tu asété”,
“il aété”,
“我们的雅芳”,
“你有空吗”,
“我是安泰”,
“j’étais”等
temps数组正在按预期进行渲染,但列表未按预期进行渲染
相反,我得到的是:
以下是我的UICollectionView方法:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var temps = [String]()
if let temp = verb {
temps = temp.temps
}
return temps.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:IndicatifCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "indicatif", for: indexPath) as! IndicatifCollectionViewCell
cell.temp.text = verb?.temps[indexPath.row]
cell.je.text = verb?.list[indexPath.row]
cell.tu.text = verb?.list[indexPath.row]
cell.il.text = verb?.list[indexPath.row]
cell.nous.text = verb?.list[indexPath.row]
cell.vous.text = verb?.list[indexPath.row]
cell.ils.text = verb?.list[indexPath.row]
return cell
}
我相信您希望在此处将列表项与临时数组项一起列出:以下是您的lsuta和临时数组:
"temps": [
"Présent",
"Passé composé",
"Imparfait",
"Plus-que-parfait",
"Passé simple",
"Passé antérieur",
"Futur",
"Futur antérieur",
"Futur proche",
"Présent",
"Passé",
"Passé - forme alternative",
"Présent",
"Imparfait",
"Plus-que-parfait",
"Passé",
"Présent",
"Présent",
"Passé"
],
"list": [
"je suis",
"tu es",
"il est",
"nous sommes",
"vous êtes",
"ils sont",
"j'ai été",
"tu as été",
"il a été",
"nous avons été",
"vous avez été",
"ils ont été",
"j'étais",....
]
您希望这些数据在一个单元格内彼此隔离。我会让结构像这样:
"finalDictionary": {
"Présent" : [
"je suis",
"tu es",
"il est",
"nous sommes",
"vous êtes",
"ils sont",
],
"Passé composé" : [
"tu as été",
"il a été",
"nous avons été",
"vous avez été",
"ils ont été",
"j'étais"
]
"Imparfait": [
"ils sont",
"j'ai été",
"tu as été",
"il a été",
"nous avons été",
]
...
..
.
}
当然,现在在numberofitemsin部分
中,您希望返回finalDictionary.allkeys().count
,并在单元格中设置值为:
if let key:String = finalDictionary()[indexPath.row] {
cell.temp.text = key
cell.je.text = finalDictionary[key][0]
cell.tu.text = finalDictionary[key][1]
cell.il.text = finalDictionary[key][2]
cell.nous.text = finalDictionary[key][3]
cell.vous.text = finalDictionary[key][4]
cell.ils.text = finalDictionary[key][6]
}
我做错了,实现这一点的简单方法是使用switch语句:
switch indexPath {
case [0, 0]:
cell.temp.text = temps[0]
cell.je.text = lists[0]
cell.tu.text = lists[1]
cell.il.text = lists[2]
cell.nous.text = lists[3]
cell.vous.text = lists[4]
cell.ils.text = lists[5]
default:
print("...")
}
依此类推。数组来自(API)。2件事使用临时数组将数据加载到集合视图。一旦临时阵列需要重新加载数据收集视图,您能更具体一点吗?我应该在哪里重新加载CollectionView,它已经在数据请求方法中加载了:DispatchQueue.main.async{self.IndicatificCollectionView.reloadData()}哦,如果我没有弄错的话,我相信您的数据结构比我想象的要复杂。这更像是一个架构决策。您应该将temps值绑定到其关联的列表项。所以这两个都是同步的。我不确定我是否理解你的意思。也许有一点解释吧!