小编Sup*_*ete的帖子

快速条件函数返回

我想知道如何快速管理有条件回报。例如,我返回一个自定义 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”调用会挂起。

谢谢你们。

return function conditional-statements ios swift

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

conditional-statements ×1

function ×1

ios ×1

return ×1

swift ×1