omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    example of a custom tableviewcell

    Pythonista
    4
    14
    7958
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • tachijuan
      tachijuan last edited by

      Hey Folks,

      I'm trying to write a simple score keeper for a pool (billiards) game that my friends and I play. I have the basic mechanics down with the default tableviewcells, but I'd like to make the result a bit prettier by using a custom tableviewcell. I've looked around github and the forums, but I can't find any examples of how to do this. Does anyone have a simple example I can copy from? I'd like to have a cell that has two text labels and an image view all in a single row.

      Any help or pointers would be very helpful.

      Thanks,

      Juan

      1 Reply Last reply Reply Quote 0
      • Dann239
        Dann239 last edited by

        import ui
        
        class source (object):
        
            def tableview_number_of_rows(self, tv, s):
                return 4
        
            def tableview_cell_for_row(self, tv, s, r):
                type = {0:'default', 1:'subtitle', 2:'value1', 3:'value2'}[r]
                cell = ui.TableViewCell(type)
                cell.text_label.text = 'Title'
                try:
                    cell.detail_text_label.text = 'Detail'
                except AttributeError:
                    pass
                try:
                    cell.image_view.image = ui.Image.named('ionicons-alert-24')
                except AttributeError:
                    pass
                return cell
        
        view = ui.TableView()
        
        view.data_source = source()
        
        view.present()
        

        author: Omega0

        1 Reply Last reply Reply Quote 0
        • tachijuan
          tachijuan last edited by

          Thanks for the pointer.

          I saw something similar, but this isn't a custom tableviewcell from what I gather. This is just a way to specify the cell type based on pre-defined cell types (subtitles, etc.). I'd like to have a custom tableviewcell class with the three views (two text labels, and one image view). Then during the tableview_cell_for_row method I would instantiate a copy of my own class instead of ui.TableViewCell().

          1 Reply Last reply Reply Quote 0
          • Omega0
            Omega0 last edited by

            You can add subviews to the content_view attribute of an instance of ui.TableViewCell. This is mentioned in the documentation but not directly, it only tells you that views can be added this way, not why you would do it. Also, Dann, for the author of the code above you could find the username of the poster. (I care only because I made it.)

            1 Reply Last reply Reply Quote 0
            • Dann239
              Dann239 last edited by

              I couldn't find the username. I had this snippet I'm my 'forum snippets' folder to learn from. I tried searching the forum posts for you. But couldn't :( sorry.

              1 Reply Last reply Reply Quote 0
              • Omega0
                Omega0 last edited by

                That's fine. It was a pretty quick code anyway.

                1 Reply Last reply Reply Quote 0
                • ?
                  A Former User last edited by

                  @tachijuan Don't know if this helps you...

                  'Classes' - SettingsSheet

                  It's switches in a cell... but you could add labels and an image instead by the same method.

                  1 Reply Last reply Reply Quote 0
                  • tachijuan
                    tachijuan last edited by

                    I think that will do it. Have a long flight tomorrow so I can play with this. Thanks for the help fellas.

                    1 Reply Last reply Reply Quote 0
                    • tachijuan
                      tachijuan last edited by

                      OK - I was able to make it work by creating a subview and adding the text/image before I added it to the tableviewcell. Still not sure how I would change it after the fact. I tried using the "name" property as the way to refer to the subview. How do I refer to the subviews after I've added them to the tableviewcell?

                      Sorry for the rookie questions, but I'm still wrapping my head around this one.

                      1 Reply Last reply Reply Quote 0
                      • ?
                        A Former User last edited by

                        @tachijuan This is rather brute force, but works. Add a list of cells as a public property, and append the cells as they are created... see updated SettingsSheet class.

                        1 Reply Last reply Reply Quote 0
                        • tachijuan
                          tachijuan last edited by

                          Cool. That is brute force. Is there no way to give the subview a "name" property so that I can do something like:

                          cell = ui.TableViewCell()
                          tl = ui.TextLabel()
                          tl.name = "hits"
                          cell.add_subview(tl)
                          cell['hits'] = "2"
                          

                          Or something like that? The docs seem to say that you can but for some reason it's not working for me.

                          1 Reply Last reply Reply Quote 0
                          • ?
                            A Former User last edited by

                            @tachijuan A ui.View can be used like a dict of it's named subviews... but I think this little test script shows that a ui.TableViewCell doesn't support that... try it with View and then TableViewCell. It looks like subviews[n] is as good as it gets.

                            import ui
                            v = ui.View()
                            #v = ui.TableViewCell()
                            b = ui.Button()
                            b.name = 'btn'
                            v.add_subview(b)
                            print v['btn'].name
                            
                            1 Reply Last reply Reply Quote 0
                            • ?
                              A Former User last edited by

                              @techijuan Ok, the trick is to add the subviews to the cell's content_view not the cell... then it works to use content_view like a dict.

                              P.S. ListDataSource also has an (undocumented ?) tableview attributute that is useful for upwards navigation

                              1 Reply Last reply Reply Quote 0
                              • tachijuan
                                tachijuan last edited by

                                aha!

                                Cool. Thank you!

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post
                                Powered by NodeBB Forums | Contributors