bcylin / QuickTableViewController

A simple way to create a UITableView for settings in Swift.

Home Page:https://bcylin.github.io/QuickTableViewController

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to disable a setting option

alamodey opened this issue · comments

Hi, I have just started using QuickTableViewController to create a settings tab for my iOS app. One of my settings (a toggle switch) is only applicable to users that have the premium version. How do I disable the switch, grey it out or hide it until the user has upgraded to Premium? Thanks.

Hi @alamodey,

There are a few ways to customise the cell. One of which is using the customize closure in SwitchRow. Another approach is to create a customised cell type:

class CustomSwitchCell: SwitchCell {

  func configure(isSwitchControlEnabled: Bool) {
    switchControl.isEnabled = isSwitchControlEnabled
  }

}

Then use this cell type in the specific SwitchRow and configure it with the state you need:

class ViewController: QuickTableViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    tableContents = [
      Section(title: "...", rows: [
        SwitchRow<CustomSwitchCell>(text: "...", switchValue: false, action: { _ in })
      ])
    ]
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = super.tableView(tableView, cellForRowAt: indexPath)
    (cell as? CustomSwitchCell)?.configure(isSwitchControlEnabled: <#T##Bool#>)
    return cell
  }

}

Hope it helps.

Thanks, that worked well!

Is there something similar I can use if I wanted to grey out a set of radio buttons?

Yes, the approach is similar to grey out radio buttons.

Use a custom OptionRowCell to configure the visual style and disable cell selections using the table view delegate method:

func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool

I'm having a bit of trouble. Do you mind showing an example. If I have a simple radio button section setup like this:

tableContents = [
     
     RadioSection(title: "VOICE (REQUIRES PREMIUM)", options: [
       OptionRow(text: "♂︎ Female", isSelected: defaults.integer(forKey: "Voice") == 0, action: didToggleSelection()),
       OptionRow(text: "♀︎ Male", isSelected: defaults.integer(forKey: "Voice") == 1, action: didToggleSelection())
       ])

But only want to enable them being activated based on a condition, what will I do?

When tableView(_:shouldHighlightRowAt:) returns false, it should disable the interaction.

final class ExampleViewController: QuickTableViewController {

  private var isRadioSectionEnabled = true {
    didSet {
      guard let index = radioSectionIndex else { return }
      tableView.reloadSections([index], with: .none)
    }
  }

  private lazy var switchSection = Section(title: "Switch", rows: [
    SwitchRow(text: "Setting 1", switchValue: true, action: didToggleSwitch())
  ])

  private lazy var radioSection = RadioSection(title: "Radio Buttons", options: [
    OptionRow<CustomOptionCell>(text: "Option 1", isSelected: true, action: { _ in }),
    OptionRow<CustomOptionCell>(text: "Option 2", isSelected: false, action: { _ in })
  ])

  private var radioSectionIndex: Int?

  override func viewDidLoad() {
    super.viewDidLoad()
    tableContents = [switchSection, radioSection]
    radioSectionIndex = tableContents.firstIndex(where: { $0 === radioSection })
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = super.tableView(tableView, cellForRowAt: indexPath)
    (cell as? CustomOptionCell)?.configureAppearance(isOptionEnabled: isRadioSectionEnabled)
    return cell
  }

  override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    if indexPath.section == radioSectionIndex {
      return isRadioSectionEnabled
    } else {
      return super.tableView(tableView, shouldHighlightRowAt: indexPath)
    }
  }

  private func didToggleSwitch() -> (Row) -> Void {
    return { [weak self] in
      guard let row = $0 as? SwitchRowCompatible else { return }
      self?.isRadioSectionEnabled = row.switchValue
    }
  }

}


private final class CustomOptionCell: UITableViewCell {

  func configureAppearance(isOptionEnabled: Bool) {}

}
commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.