我在我的 UICollectionView 中总共使用了 15 个静态行。如果我单击每个项目,它将导航到关联的 UIViewController。首先我使用了 If-else,但现在我不使用 If-else。我正在为此使用 switch。但我怀疑我正在为此编写 15 个案例的 15 行。但我想减少我的代码。有没有减少代码的逻辑。
我的代码在 dis 中
switch(indexPath.row)
{
case 1:
break
case 2:
break
case 3 :
break
.
.
.
case 15 :
break
default :
print( "default case")
}
谁能帮我解决这个问题。 提前致谢...
最佳答案
将 View Controller 的 Identifier
保存到数组中。单击单元格时,从数组中获取标识符并推送特定的 View Controller 。
let arrayTest : NSArray = ["test1", "test2"]// Declared this on view did load
//Write below code on did select delegate method
let identifier = arrayTest.objectAtIndex(indexPath.row)
let viewController = UIStoryboard.instantiateViewControllerWithIdentifier(identifier)
self.navigationController?.pushViewController(viewController, animated: true)
关于ios - 如何在 swift 中使用 switch 显示 15 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38887689/