Hi, I just refactored this to widget2.0 and if the listView is in any position besides being scrolled to the bottom, it constantly calls onRowRender() . Which worries me if I actually go to do something here instead of teh examples. Weird. Im kinda noob here so should I log a bug or can someone help spot my mistake? Also not a pro programmer so any help appreciated. I tried to comment out any activity that might call onRowRender:
–
– sceneQuote.lua
–
print(“at all in?”)
local widget = require( “widget” )
print(“widget”)
local storyboard = require( “storyboard” )
print(“storyboard”)
–local automask = require( “AutoMask” )
local database = require( “DataBase” )
print(“db”)
local scene = storyboard.newScene()
print(“scene”)
require “sqlite3”
local image
–local list
local db
local backButton
local addButton
local sbHeight = display.statusBarHeight
local tbHeight = 44
local listViewTop = sbHeight + tbHeight
– create a gradient for the top-half of the toolbar
local toolbarGradient = graphics.newGradient( {168, 181, 198, 255 }, {139, 157, 180, 255}, “down” )
print(“locals loaded”)
–
– Touch event listener for background image
local function onSceneTouch( self, event )
if event.phase == “began” then
– storyboard.gotoScene( “screen2”, “slideLeft”, 800 )
return true
end
end
print(“onSceneTouch loaded”)
– The gradient used by the title bar
local titleGradient = graphics.newGradient( { 189, 203, 220, 255 }, { 89, 116, 152, 255 }, “down” )
local titleBar = display.newRect( 0, sbHeight, display.contentWidth,tbHeight )
–titleBar.y = titleBar.contentHeight * 0.5
titleBar:setFillColor( titleGradient )
– Create the title bar text
local titleBarText = display.newText( “Working Projects”, 0, 0, native.systemFontBold, 16 )
titleBarText.x = titleBar.x
titleBarText.y = titleBar.y
print(“titleBar loaded”)
– create a shadow underneath the titlebar (for a nice touch)
local shadow = display.newImage( “assets/shadow.png” )
shadow:setReferencePoint( display.TopLeftReferencePoint )
shadow.x, shadow.y = 0, listViewTop
shadow.xScale = display.contentWidth / shadow.contentWidth
shadow.alpha = 0.45
local top = display.statusBarHeight*2
local list = nil
print(“shadow loaded”)
–Text to show which item we selected
local itemSelected = display.newText( “You selected”, 0, 0, native.systemFontBold, 24 )
itemSelected:setTextColor( 0 )
itemSelected.x = display.contentWidth + itemSelected.contentWidth * 0.5
itemSelected.y = display.contentCenterY
print(“itemSelected loaded”)
local topBoundary = display.screenOriginY + 60
local bottomBoundary = display.screenOriginY -290
–Items to show in our list
local listItems = {
{ title = “A”, items = { } },
{ title = “B”, items = { “ExampleA”, “ExampleB”,“ExampleC”,“ExampleD” } },
{ title = “C”, items = { } },
{ title = “D”, items = { } },
{ title = “E”, items = { “ExampleA”, “ExampleB”,“ExampleC”,“ExampleD” } },
– { title = “E”, items = { } },
{ title = “F”, items = { } },
{ title = “G”, items = { } },
{ title = “H”, items = { “ExampleA”, “ExampleB”,“ExampleC”,“ExampleD” } },
{ title = “I”, items = { } },
{ title = “J”, items = { } },
{ title = “K”, items = { } },
{ title = “L”, items = { “ExampleA”, “ExampleB”,“ExampleC”,“ExampleD” } },
{ title = “M”, items = { } },
{ title = “N”, items = { } },
{ title = “O”, items = { } },
{ title = “P”, items = { } },
{ title = “Q”, items = { } },
{ title = “R”, items = { } },
{ title = “S”, items = { } },
{ title = “T”, items = { } },
{ title = “U”, items = { } },
{ title = “V”, items = { } },
{ title = “W”, items = { } },
{ title = “X”, items = { } },
{ title = “Y”, items = { } },
{ title = “Z”, items = { } },
}
–Used for displaying row text
local rowTitles = {}
print(“titles and table loaded”)
–Handle row rendering
local function onRowRender( event )
local phase = event.phase
local row = event.row
local label = rowTitles[row.index]
if row.isCategory then
color = 255
end
print("rendering row: ")
--Set the row’s name
local rowtextObj = display.newText( row, label, 0, 0, nil, 14 )
rowtextObj.x = row.x - ( row.contentWidth * 0.5 ) + ( rowtextObj.contentWidth * 0.5 )
rowtextObj.y = row.contentHeight * 0.5
rowtextObj:setTextColor( 0, 0, 0 )
end
print(“onRowRender loaded”)
local function findPlacement(str)
i = 1
while true do
if string.sub(string.upper(listItems[i].title),0,1) == string.sub(string.upper(str),0,1) then break end
– print(“comparing " … listItems[i].title … " to :” … str)
i = i + 1
end
return i
end
–Handle the back button release event
local function onBackRelease()
--Transition in the list, transition out the item selected text and the back button
transition.to( list, { x = 0, time = 400, transition = easing.outExpo } )
transition.to( itemSelected, { x = display.contentWidth + itemSelected.contentWidth * 0.5, time = 400, transition = easing.outExpo } )
transition.to( backButton, { x = 60, alpha = 0, time = 400, transition = easing.outQuad } )
end
function tableLength(t)
if type(t) == “table” then
local count = 0
for _, _ in pairs(t) do count = count + 1 end
return count
end
end
function onAddRelease(event)
--addButton.alpha = 0
storyboard.showOverlay( “sceneNewProj”, { effect=“fade”, params={}, isModal=true } )
end
–Create the back button
backButton = widget.newButton{
style = “backSmall”,
label = “Back”,
yOffset = - 2,
onEvent = onBackRelease
}
backButton.alpha = 0
backButton.x = 80
backButton.y = titleBar.y
– create save button (top right)
–addButton = widget.newButton{
– style = “blue1Small”,
– label = “Add”,
– yOffset = - 2,
– onEvent = onAddRelease,
–}
–addButton.alpha = 1
–addButton.x = 40
–addButton.y = titleBar.y
–Hande row touch events
local function onRowTouch( event )
local row = event.row
local background = event.background
if event.phase == “press” then
print( "Pressed row: " … row.index )
background:setFillColor( 0, 110, 233, 255 )
elseif event.phase == “release” or event.phase == “tap” then
--Update the item selected text
itemSelected.text = "You selected " … row.itemName
– Transition out the list, transition in the item selected text and the back button
– transition.to( list, { x = - list.contentWidth, time = 400, transition = easing.outExpo } )
– transition.to( itemSelected, { x = display.contentCenterX, time = 400, transition = easing.outExpo } )
– transition.to( backButton, { x = 40, alpha = 1, time = 400, transition = easing.outQuad } )
print( "Tapped and/or Released row: " … row.index )
background:setFillColor( 0, 110, 233, 255 )
– row.reRender = true
end
end
– Handle row updates
local function onRowUpdate( event )
– local phase = event.phase
– local row = event.row
–
– print( row.index, “: is now onscreen” )
end
local function tableViewListener( event )
– local phase = event.phase
–
– print( “Event.phase is:”, event.phase )
end
local function fillRows()
– insert rows into list (tableView widget)
print(“filling rows”)
for i = 1, #listItems do
--Add the rows category title
rowTitles[#rowTitles + 1] = listItems[i].title
--Insert the category
local isCategory = true
local rowHeight = 24
local rowColor =
{
default = { 150, 160, 180, 200 },
}
local lineColor = { 220, 220, 220 }
list:insertRow{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
print("inserted category: "…listItems[i].title)
– Insert the item
for j = 1, #listItems[i].items do
– sort the rows
table.sort(listItems[i])
– Add the rows item title
rowTitles[#rowTitles + 1] = listItems[i].items[j]
– Insert the item
list:insertRow{
isCategory = false,
rowHeight = 40,
rowColor =
{
default={ 255, 255, 255 },
over={ 30, 144, 255 },
},
lineColor = { 220, 220, 220 },
}
print(“inserted item”)
end
end
end
local function createNewProject(name)
local nameRow = storyboard.state.newProjectName
– add text fielf to collect new name
local position = 0
position = findPlacement(nameRow)
--print("Position of the new string is: " … listItems[position].title)
local itemindex = tableLength(listItems[position].items)
listItems[position].items[itemindex+1] = nameRow
database:createNewProjectByName(nameRow)
list:deleteAllRows()
rowTitles = {}
fillRows()
– reset the new proj name
storyboard.state.newProjectName = “”
end
list = widget.newTableView {
width = display.contentWidth,
left = 0,
top = listViewTop,
height = display.contentHeight,
bottomPadding = 8,
--maskFile = “assets/mask454.png”,
--maskFile = automask:getMask( display.contentWidth, display.contentHeight, nil, nil, true )
listener = tableViewListener,
onRowRender = onRowRender,
onRowUpdate = onRowUpdate,
onRowTouch = onRowTouch,
}
list.x = 1
list.y = titleBar.y + titleBar.contentHeight * 0.5
print(“tView loaded”)
– Called when the scene’s view does not exist:
function scene:createScene( event )
print( “\n3: entering createScene Project event”)
screenGroup = self.view
if (_G.debug) then
image = display.newRect(0, 0, display.contentWidth, display.contentHeight)
image:setFillColor(255, 255, 255)
screenGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( “Quote”, 0, 0, native.systemFontBold, 24 )
text1:setTextColor( 0 )
text1:setReferencePoint( display.CenterReferencePoint )
text1.x, text1.y = display.contentWidth * 0.5, 50
screenGroup:insert( text1 )
text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 )
text2:setTextColor( 0 )
text2:setReferencePoint( display.CenterReferencePoint )
text2.x, text2.y = display.contentWidth * 0.5, display.contentHeight - 90
screenGroup:insert( text2 )
text3 = display.newText( “Debug Mode”, 0, 0, native.systemFontBold, 18 )
text3:setTextColor( 0 ); text3.isVisible = false
text3:setReferencePoint( display.CenterReferencePoint )
text3.x, text3.y = display.contentWidth * 0.5, display.contentHeight - 70
screenGroup:insert( text3 )
end
local bg = display.newImageRect( “assets/Default@2.png”, display.contentWidth, display.contentHeight )
bg.x = display.contentWidth / 2
bg.y = display.contentHeight / 2 --+ display.statusBarHeight
screenGroup:insert(bg)
screenGroup:insert(shadow)
screenGroup:insert(titleBar)
screenGroup:insert(list)
– screenGroup:insert(titleText)
– screenGroup:insert( itemSelected )
– screenGroup:insert( backButton )
– screenGroup:insert( addButton )
print(“prepping to fill rows”)
fillRows()
---- create scene
print( “\n3: createScene Project event”)
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( “\n1: enterScene Project event” )
screenGroup = self.view
if storyboard.state.newProjectName ~= “” then
createNewProject()
end
– addButton.alpha = 1
if (_G.debug) then
– Update Lua memory text display
local showMem = function()
image:addEventListener( “touch”, image )
text3.isVisible = true
text2.text = text2.text … collectgarbage(“count”)/1000 … “MB”
text2.x = display.contentWidth * 0.5
end
memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
print( “Project: end enter Project event” )
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
print( “Project: start exitScene event” )
if storyboard.state.newProjectName ~= “” then
createNewProject()
end
--addButton.alpha = 1
--storyboard.purgeScene( “sceneQuotes” )
print( “Project: leaving exitScene event” )
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
print( “((destroying scene Project’s view))” )
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene