Using drawCell override with object subclasses?

I'm curious if anybody has advice on how to best work with the gridview override functions inside of an object subclass? It's a few times now where I've wanted to do something like:

function self.listView:drawCell(section, row, column, selected, x, y, width, height)
	gfx.drawTextInRect(self.optionsList[row], x, y, width, height)
end

But instead need to redeclare that value first to access it properly:

local text = self.optionsList
function self.listView:drawCell(section, row, column, selected, x, y, width, height)
	gfx.drawTextInRect(text[row], x, y, width, height)
end

Maybe I'm missing a key concept of the SDK's implementation of subclasses here, though. Let me know if anyone else has come across this. Thanks!

2 Likes

A couple idea pop to mind, nothing 'good' though :playdate_agh:

  • could you subclass gridview? might require a bigger refactor but might work.
  • you could back reference, ie: self.listview.parent = self and then use that in the override function

other that that I'm not sure :playdate_question:

1 Like

Yes, this was the first thing I thought of as well! I wanted to avoid it...

I'll try the back reference and report back, thanks Rob !

Did you find any other ways? For anyone else finding this thread like I did, the back reference did work for me with something like this (I had it a bit more convoluted but this is the gist):

function Base:init()
    Base.super.init(self)

    self.optionsList = { ... }

    self.listview = playdate.ui.gridview.new(0, 100)
    self.listview.parent = self

    -- Note self as first arg, it will refer to self.listview when the function is called!
    self.listview.drawCell = function(self, section, row, column, selected, x, y, width, height)
        self.parent.optionsList[row] [...]
    end