Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Implementing a radio style (single selected option) check mark in a forms dialog
-
@madivad said:
What is the method that is called within the form controller that would need to be overwritten/created?
I don't understand the question if it is relative to the switch because your sample already contains a switch.
-
And without your own form_dialog....but it has been hard to find 😅
def segmented_action(sender): global c if sender.selected_index >= 0: c.values[sender.name] = sender.segments[sender.selected_index] def my_tableview_cell_for_row(self,tv, section, row): global c c = self cell = self.cells[section][row] if len(cell.content_view.subviews) > 0: tf = cell.content_view.subviews[0] # ui.TextField of value in row item = self.sections[section][1][row] if 'segments' in item: # check if SegmentedControl already added for sv in cell.content_view.subviews: if type(sv) is ui.SegmentedControl: return cell segmented = ui.SegmentedControl() segmented.name = cell.text_label.text segmented.action = segmented_action segmented.frame = tf.frame segmented.x = c.view.width - segmented.width - 8 segmented.segments = item['segments'] cell.content_view.add_subview(segmented) elif isinstance(tf, ui.TextField): tf.alignment=ui.ALIGN_RIGHT return cell dialogs._FormDialogController.tableview_cell_for_row = my_tableview_cell_for_row diag = dialogs.form_dialog(title = 'Form Dialog', sections=form_list_of_sections)
-
The last code is not correct if you scroll the dialog so the Segments are hidden and reshown back because the code adds a new subview each time. Let me some time to correct it.
That's the problem when you do this process in tableview_cell_for_row -
Code corrected (and edited in the post), sorry for that
-
And if you want a dialog field as a button (radio, checkbox, ...), this code could help you.
You have to design the field with two images for your button, like:sectionA_dicts.append(dict(type = 'text', title = 'My button', key = 'button', button = ['iob:ios7_checkmark_32','iob:ios7_checkmark_outline_32']))
and the associated process:
def button_action(sender): global c sender.idx = 1 - sender.idx sender.image = ui.Image.named(sender.images[sender.idx]) c.values[sender.name] = sender.idx def my_tableview_cell_for_row(self,tv, section, row): global c c = self cell = self.cells[section][row] if len(cell.content_view.subviews) > 0: tf = cell.content_view.subviews[0] # ui.TextField of value in row item = self.sections[section][1][row] if 'segments' in item: # check if SegmentedControl already added for sv in cell.content_view.subviews: if type(sv) is ui.SegmentedControl: return cell segmented = ui.SegmentedControl() segmented.name = cell.text_label.text segmented.action = segmented_action segmented.frame = tf.frame segmented.x = c.view.width - segmented.width - 8 segmented.segments = item['segments'] cell.content_view.add_subview(segmented) elif 'button' in item: # check if Button already added for sv in cell.content_view.subviews: if type(sv) is ui.Button: return cell button = ui.Button() button.name = cell.text_label.text button.action = button_action button.frame = tf.frame button.width = button.height button.x = c.view.width - button.width - 8 button.images = item['button'] button.title = '' button.idx = 0 button.image = ui.Image.named(button.images[button.idx]) cell.content_view.add_subview(button) elif type(tf) is ui.TextField: tf.alignment=ui.ALIGN_RIGHT return cell
-
and supporting color images in button:
. . . sectionA_dicts.append(dict(type = 'text', title = 'My face', key = 'face', button = ['emj:Smiling_1','emj:Disappointed'])) . . . def button_action(sender): global c sender.idx = 1 - sender.idx sender.image = ui.Image.named(sender.images[sender.idx]).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL) c.values[sender.name] = sender.idx . . . button.image = ui.Image.named(button.images[button.idx]).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL) . . .
-
@cvp you are incredible...
I saw your first posts on the way to work and thought, “yeah, I have to get to that!” And now I log back and on and find this!!!! MUCH to digest!
I will be back (busy the next few days, but the kids are on holidays after tomorrow and I’ll slow down on the workload, so I should be able to get into this more)
Seriously, thanks for your contributions, you are awesome!
-
@cvp I finally got back to this, I got it all working as expected, the only issue I have is with the custom rows (My segmented control*, My button, and My face), they allow the cursor into the row label on the left hand side if the user touches near them.
I’ve spent a couple of hours going over the cradle and only minimally beginning to understand it :)
* the segmented control is ok in the custom/my_form_dialog and doesn’t allow the cursor in. It only allows the cursor when using the inbuilt dialog.
-
@madivad It is normal. When we create the new fields types in tableview_cell_for_row, the original TextField still exists. In the other case, the code deletes the TextField.
-
@madivad you can add these lines when you create a new field type
aftercell.content_view.add_subview(segmented)
Add
cell.content_view.remove_subview(tf) del self.values[tf.name] del tf
-
@cvp said:
cell.content_view.remove_subview(tf) del self.values[tf.name] del tf
I knew it was something like that, I was giving it a go but hadn’t got it right.
Thanks.