我想知道如何快速管理有条件回报。例如,我返回一个自定义 UICollectionViewCell,具体取决于调用哪个 collectionview 委托:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if (collectionView.isEqual(collectionView1)) {
var cell = self.epgCollectionView.dequeueReusableCellWithReuseIdentifier("Cell1", forIndexPath: indexPath) as Cell1
return cell
}
else if (collectionView.isEqual(collectionView2)) {
var cell = self.epgCollectionView.dequeueReusableCellWithReuseIdentifier("Cell2", forIndexPath: indexPath) as Cell2
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
编译器说“函数中缺少 return 语句期望返回 UICollectionViewCell”,即使在这两种情况下我都返回一个单元格。
我解决了它添加
return UICollectionViewCell()
Run Code Online (Sandbox Code Playgroud)
在函数的底部,但我认为这不是正确的方法。
我知道我可以在第一个“if”上方声明单元格,修改它并在“if”之外的函数末尾返回它,但随后“dequeueReusableCellWithIdentifier”调用会挂起。
谢谢你们。