在我的第一部分中,我UIAlertController
根据行展示了不同的样式。第二部分做不相关的事情。为了避免两个case
s 中的代码重复,如何在 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
}
}