我的cellForItem遇到了一些麻烦,因为我不知道如何同时加载3个集合视图。这是我尝试过的方法,但出现错误“在预期返回'UICollectionViewCell的函数中缺少返回”。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.lostCollectionView {
let lostcell: LostCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Lostcell", for: indexPath) as! LostCollectionViewCell
lostcell.set(post: posts[indexPath.row])
//Make TextView Clickable
lostcell.phoneLostTextView.isEditable = false;
lostcell.phoneLostTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return lostcell
}
if collectionView == self.foundCollectionView {
let foundcell: FoundCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Foundcell", for: indexPath) as! FoundCollectionViewCell
foundcell.set(postfound: postsfound[indexPath.row])
//Make TextView Clickable
foundcell.phoneFoundTextView.isEditable = false;
foundcell.phoneFoundTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return foundcell
}
if collectionView == self.adoptionCollectionView {
let adoptioncell: AdoptionCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Adopotioncell", for: indexPath) as! AdoptionCollectionViewCell
adoptioncell.set(postadoption: postsadoption[indexPath.row])
//Make TextView Clickable
adoptioncell.phoneAdoptionTextView.isEditable = false;
adoptioncell.phoneAdoptionTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return adoptioncell
}
}
答案 0 :(得分:3)
您的函数有3个if
。如果它们全部失败,则该函数将不返回任何内容。这就是Swift编译器抱怨的原因。
您可以在函数底部添加return UICollectionViewCell()
。
此外,switch
语句更适合这种情况。
答案 1 :(得分:0)
如意义要点所述,这是由于缺少三个if
的情况下缺少单元格所致。
您可以按照功能含义的建议在函数底部添加一个最终的后备单元格。
或者,您可以采用我个人更喜欢的另一种类似方法:
// Inside your **cellForItemAt** function.
switch collectionView {
case lostCollectionView:
// Replace this with your corresponding code.
return LostCollectionViewCell()
case foundCollectionView:
// Replace this with your corresponding code.
return FoundCollectionViewCell()
case adoptionCollectionView:
// Replace this with your corresponding code.
return AdoptionCollectionViewCell()
default:
return UICollectionViewCell()
}
我个人认为,这种情况下的开关柜是更整洁的解决方案。
答案 2 :(得分:0)
基本上,您需要将代码包含在这样的switch语句中:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch collectionView {
case self.lostCollectionView:
let lostcell: LostCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Lostcell", for: indexPath) as! LostCollectionViewCell
lostcell.set(post: posts[indexPath.row])
//Make TextView Clickable
lostcell.phoneLostTextView.isEditable = false;
lostcell.phoneLostTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return lostcell
case self.foundCollectionView:
let foundcell: FoundCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Foundcell", for: indexPath) as! FoundCollectionViewCell
foundcell.set(postfound: postsfound[indexPath.row])
//Make TextView Clickable
foundcell.phoneFoundTextView.isEditable = false;
foundcell.phoneFoundTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return foundcell
case self.adoptionCollectionView:
let adoptioncell: AdoptionCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Adopotioncell", for: indexPath) as! AdoptionCollectionViewCell
adoptioncell.set(postadoption: postsadoption[indexPath.row])
//Make TextView Clickable
adoptioncell.phoneAdoptionTextView.isEditable = false;
adoptioncell.phoneAdoptionTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return adoptioncell
default:
return UICollectionViewCell()
}
}