UIAlertController
最初のセクションでは、行に基づいて異なるスタイルを示します。2 番目のセクションでは、無関係なことを行います。両方の s でコードの重複を避けるためにcase
、switch ステートメントで特定のケースにフォールスルーするにはどうすればよいですか? これは迅速に可能ですか?他の言語にこの概念がありますか?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var alertController: UIAlertController!
let cancelAction = UIAlertAction(title: L10n.Cancel.localized, style: .Cancel) { (action) in
// ...
}
switch (indexPath.section, indexPath.row) {
case (0, 0):
alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
//add other actions
case (0, 1):
alertController = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
//add other actions
case (0, _): //this case handles indexPath.section == 0 && indexPath.row != 0 or 1
//I want this to be called too if indexPath.section is 0;
//even if indexPath.row is 0 or 1.
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
default:
break
}
}