Ios 使用不同的数据重用UITableViewCell
我有两个不同数据的数组Ios 使用不同的数据重用UITableViewCell,ios,swift,uitableview,uicollectionview,uicollectionviewcell,Ios,Swift,Uitableview,Uicollectionview,Uicollectionviewcell,我有两个不同数据的数组 var array : [String] = ["getMeals", "getMeasure", "getWorkouts"] var array2 : [String] = ["getStep", "getStep", "getStep"] 我试图从数组中检索图像,但在不同的收集单元中 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
var array : [String] = ["getMeals", "getMeasure", "getWorkouts"]
var array2 : [String] = ["getStep", "getStep", "getStep"]
我试图从数组中检索图像,但在不同的收集单元中
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkoutCollectionCell", for: indexPath) as! WorkoutCollectionCell
cell.collectionImage.image = UIImage(named: array[indexPath.row])
return cell
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkoutCollectionCell", for: indexPath) as! WorkoutCollectionCell
cell.collectionImage.image = UIImage(named: array2[indexPath.row])
return cell
}
}
我需要创建另一个UICollectionViewCell,或者我可以使用当前单元格?您可以使用相同的单元格,代码如下:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkoutCollectionCell", for: indexPath) as! WorkoutCollectionCell
if indexPath.section == 0{
cell.collectionImage.image = UIImage(named: array[indexPath.row])
}else{
cell.collectionImage.image = UIImage(named: array2[indexPath.row])
}
return cell
}
您可以使用相同的单元格,代码如下:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkoutCollectionCell", for: indexPath) as! WorkoutCollectionCell
if indexPath.section == 0{
cell.collectionImage.image = UIImage(named: array[indexPath.row])
}else{
cell.collectionImage.image = UIImage(named: array2[indexPath.row])
}
return cell
}
你需要一个像这样的数组
let array = [["getMeals", "getMeasure", "getWorkouts"],["getStep", "getStep", "getStep"]]
func numberOfSections(in collectionView: UICollectionView) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkoutCollectionCell", for: indexPath) as! WorkoutCollectionCell
cell.collectionImage.image = UIImage(named: array[indexPath.section][indexPath.row])
return cell
}
你需要一个像这样的数组
let array = [["getMeals", "getMeasure", "getWorkouts"],["getStep", "getStep", "getStep"]]
func numberOfSections(in collectionView: UICollectionView) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkoutCollectionCell", for: indexPath) as! WorkoutCollectionCell
cell.collectionImage.image = UIImage(named: array[indexPath.section][indexPath.row])
return cell
}
简短的回答是“是”,首先将单元格出列,根据您想要的任何条件分配内容,然后返回单元格。简短的回答是“是”,首先将单元格出列,根据您想要的任何条件分配内容,然后返回单元格。但现在我有2个collectionViewCells,其中包含6个图像。2个是什么?您只能使用id
WorkoutCollectionCell
将1出列。您不能将集合单元格用于表格和表格,但现在我有2个集合视图单元格,其中包含6个图像2在哪里?您只能使用idWorkoutCollectionCell
将1出列,不能将收集单元格用于表,反之亦然