Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to disable a setting option #57

Closed
alamodey opened this issue Jul 26, 2020 · 7 comments
Closed

How to disable a setting option #57

alamodey opened this issue Jul 26, 2020 · 7 comments
Labels

Comments

@alamodey
Copy link

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.

@bcylin
Copy link
Owner

bcylin commented Jul 26, 2020

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.

@alamodey
Copy link
Author

Thanks, that worked well!

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

@bcylin
Copy link
Owner

bcylin commented Jul 27, 2020

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

@alamodey
Copy link
Author

alamodey commented Aug 4, 2020

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?

@bcylin
Copy link
Owner

bcylin commented Aug 4, 2020

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) {}

}

@stale
Copy link

stale bot commented Oct 3, 2020

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.

@stale stale bot added the wontfix label Oct 3, 2020
@bcylin bcylin added stale and removed wontfix labels Oct 11, 2020
@stale stale bot removed the stale label Oct 11, 2020
@stale
Copy link

stale bot commented Oct 25, 2020

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.

@stale stale bot added the stale label Oct 25, 2020
@stale stale bot closed this as completed Nov 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants