Skip to content

Instantly share code, notes, and snippets.

@rockitude
Created October 13, 2013 03:22
Show Gist options
  • Save rockitude/6957769 to your computer and use it in GitHub Desktop.
Save rockitude/6957769 to your computer and use it in GitHub Desktop.
Corona SDK: TableView Widget Example Code
--
-- Project: Ch11.3 Widgets -TableView
-- Description:
--
-- Version: 1.0
-- Managed with http://CoronaProjectManager.com
--
-- Copyright 2013 Brian Burton. All Rights Reserved.
--
-- Create 100 rows, and two categories to the tableView:
local widget = require( "widget" )
-- Listen for tableView events
local function tableViewListener( event )
local phase = event.phase
print( event.phase )
end
-- Handle row rendering
local function onRowRender( event )
local phase = event.phase
local row = event.row
local rowTitle = display.newText( row, "Row " .. row.index, 0, 0, nil, 14 )
rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )
rowTitle.y = row.contentHeight * 0.5
rowTitle:setTextColor( 0, 0, 0 )
end
-- Handle row's becoming visible on screen
local function onRowUpdate( event )
local row = event.row
print( "Row:", row.index, " is now visible" )
end
-- Handle touches on the row
local function onRowTouch( event )
local phase = event.phase
if "press" == phase then
print( "Touched row:", event.target.index )
end
end
-- Create a tableView
local tableView = widget.newTableView
{
top = 100,
width = 320,
height = 510,
maskFile = "mask-410.png",
listener = tableViewListener,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}
-- Create 100 rows
for i = 1, 100 do
local isCategory = false
local rowHeight = 40
local rowColor =
{
default = { 255, 255, 255 },
}
local lineColor = { 220, 220, 220 }
-- Make some rows categories
if i == 25 or i == 50 or i == 75 then
isCategory = true
rowHeight = 24
rowColor =
{
default = { 150, 160, 180, 200 },
}
end
-- Insert the row into the tableView
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
end
-- delete the tenth row in the tableView
tableView:deleteRow( 10 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment