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
Comments
Hi @alamodey, There are a few ways to customise the cell. One of which is using the 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:
But only want to enable them being activated based on a condition, what will I do? |
When 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) {}
} |
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. |
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. |
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.
The text was updated successfully, but these errors were encountered: