extension ActionSheetView: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
-> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
let item = items[indexPath.row]
switch item {
case let .Option(title, titleColor, action):
let cell = tableView.dequeueReusableCell(withIdentifier:
ActionSheetOptionCell.reuseIdentifier, for: indexPath) as!
ActionSheetOptionCell
cell.colorTitlelabel.text = title
cell.colorTitleLabeltextColor = titleColor
cell.action = action
return cell
case let .Default(title, titleColor, _):
let cell = tableView.dequeueReusableCell(withIdentifier:
ActionSheetDefaultCell.reuseIdentifier) as! ActionSheetDefaultCell
cell.colorTitlelabel.text = title
cell.colorTitleLabeltextColor = titleColor
return cell
case let .Detail(title, titleColor, action):
let cell = tableView.dequeueReusableCell(withIdentifier:
ActionSheetDetailCell.reuseIdentifier) as! ActionSheetDetailCell
cell.textLabel?.text = title
cell.textLabel?.textColor = titleColor
cell.action = action
return cell
case let .Switch(title, titleColor, switchOn, action):
let cell = tableView.dequeueReusableCell(withIdentifier:
ActionSheetSwitchCell.reuseIdentifier) as! ActionSheetSwitchCell
cell.textLabel?.text = title
cell.textLabel?.textColor = titleColor
cell.checkedSwitch.isOn = switchOn
cell.action = action
return cell
case let .SubtitleSwitch(title, titleColor, subtitle, subtitleColor,
switchOn, action):
let cell = tableView.dequeueReusableCell(withIdentifier:
ActionSheetSubtitleSwitchCell.reuseIdentifier) as! ActionSheetSubtitleSwitchCell
cell.titleLabel.text = title
cell.titleLabel.textColor = titleColor
cell.subtitleLabel.text = subtitle
cell.subtitleLabel.textColor = subtitleColor
cell.checkedSwitch.isOn = switchOn
cell.action = action
return cell
case let .Check(title, titleColor, checked, _):
let cell = tableView.dequeueReusableCell(withIdentifier:
ActionSheetCheckCell.reuseIdentifier) as! ActionSheetCheckCell
cell.colorTitleLabel.text = title
cell.colorTitleLabelTextColor = titleColor
cell.checkImageView.isHidden = !checked
return cell
case .Cancel:
let cell = tableView.dequeueReusableCell(withIdentifier:
ActionSheetDefaultCell.reuseIdentifier) as! ActionSheetDefaultCell
cell.colorTitlelabel.text = NSLocalizedString("取消", comment: "")
cell.colorTitleLabeltextColor = UIColor.cubeTintColor()
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
defer {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
let item = items[indexPath.row]
switch item {
case .Option(_, _, let action):
hideAndDo(afterHideAction: action)
case .Default(_, _, let action):
if action() {
hide()
}
case .Detail(_, _, let action):
hideAndDo(afterHideAction: action)
case .Switch:
break
case .SubtitleSwitch:
break
case .Check(_, _, _, let action):
action()
hide()
case .Cancel:
hide()
break
}
}