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.
How to highlight a Tableview Row
-
<p>I am trying to highlight the first row in a tableview to indicate that it is the default selection in the table. I have tried variations on the following with no success. I wonder if you could tell me what I am doing wrong. Thanks.</p>
<pre><code> import ui
def action(sender):
v = tv.selected_rowwindow = ui.load_view()
window.present('popover', popover_location=(150,500))
tv = window['tv']lds = tv.data_source
lds.items = 'Sun Mon Tue Wed Thu Fri Sat'.split()
tv.bg_color = 'green' # this workscell = lds.tableview_cell_for_row(tv, 0, 0)
cell.bg_color ='red
bgc = ui.View(bg_color = 'blue')
cell.selected_background_view = bgc
lds.reload()
</code></pre> -
Tableview_cell_for_row
gets called when a cell comes into view on the screen. It dynamically creates its contents, so calling it directly doesn't work. You must write your own "lds" and include your version of ```tableview_cell_for_row``. Then you can manipulate the color, font. Here's a snippet of my flashcard program and how I highlight the chosen cell. highlight is a global which has the index of the cell to highlight. Here's also where I turn on a checkmark for that cell. You can do these things independelty. Line 221 is where the LDS class is defined and line 347 is where it gets associated with the relevant TableView as its delegate and data_source.def tableview_cell_for_row(self, tableview, section, row): # Create and return a cell for the given section/row cell = ui.TableViewCell() cell.text_label.text = self.items[row]['title'] if row == highlight: cell.text_label.text_color = 'red' cell.accessory_type = self.items[row]['accessory_type'] return cell
-
@polymerchm Thank you. I'm still at sea as to how you know that 'Tableview_cell_for_row gets called when a cell comes into view on the screen.' Is it experience or is there documentation or a book that I could reference? I've never done ui programming so any direction you might provide would be much appreciated. I looked at the objective-C documentation but it's too rich for my level of understanding. In trying to understand what was going on, I had noted your statement:
if row == highlight:
and wondered where 'highlight' originates.
Thanks again. -
Look at the GitHub site. Selected is a list built in the
did_select
part of the class I mention above. Theon_next
action loads highlight which is is a global.Much of the wisdom is from this forum. I experiment a lot. It's in the documentation, but you have to generally understand GUI programming for it to make sense. Ask questions and make mistakes. Then again, I started programming in 1968. Fortran II. Swore I'd never learn object-oriented programming. Still learning.
-
Thank you '68. Still learning as well. '71.
-
Long winded answer, and my take on what's happening.
Basically, the ui's job is to continuously redraw the screen and wait for touches, which the code you write responds too. The ui is an infinite loop of code initiated when you call
view.present()
In the loop, the underlying machinery of the ui looks for indications it needs to interact with the user.
For the view and of its subviews, it calls the view's
draw()
method which renders that view's image. Other than custom views, you don't worry aboutdraw()
.If a the view is a TableView and its had its
reload_data()
method called, the visible "cells" are redrawn with the new contents of their (or their delegate's)items
attribute. When a cell is to be rendered for such a list, it'stableview_cell_for_row()
method handles the cell-by-cell details. This is why, by knowing what cell its rendering, you can manipulate each cell differently.If the views are "touch" active (buttons usually), then the view's action is called, and is passed the object (the button) as an argument. The action (call-back) can then service the touch.
For a list view, if you touch a cell, the TableView delegate's
did_select()
is called. Touches accumulate in a list and are processed first-in, last out.Much more, hopefully that gets you started. Happy coding.