I try to amend the scrolling list views example, based on the top tutorial posted by Gilbert "CoffeDemo in sourceCode).
My goal: firts, the list is empty and an “addbutton”, when pressed should add a new row (then I will add an “editButton” to delete rows).
My code below adds effectively rows but is not completely correct; when button pressed:
- 1st time: row1 appears: ok
- 2nd time: 2nd row appears, but behind row1, a second row1 appears
- 3rd time: 3rd row appears, but behind row1, a second row1 appears and behind row2, a second row2.
- etc.
Thanks to point my mistake.
[lua]–FUNCTION TO INCREMENT THE LIST WHEN BUTTON PRESSED
local i = 1
local function addBtnRelease ( event )
data[i] = {}
data[i].title = "Iced Coffee " … i
data[i].subtitle = "Chilled coffee with ice " … i
data[i].image = “coffee2.png”
i = i + 1
– create the list of items
myList = tableView.newList{
data=data,
default=“listItemBg.png”,
–default=“listItemBg_white.png”,
over=“listItemBg_over.png”,
onRelease=listButtonRelease,
top=topBoundary,
bottom=bottomBoundary,
backgroundColor={ 255, 255, 255 }, --commented this out because we’re going to add it down below
callback = function( row )
local g = display.newGroup()
local img = display.newImage(row.image)
g:insert(img)
img.x = math.floor(img.width*0.5 + 6)
img.y = math.floor(img.height*0.5)
local title = display.newText( row.title, 0, 0, native.systemFontBold, 14 )
title:setTextColor(0, 0, 0)
–title:setTextColor(255, 255, 255)
g:insert(title)
title.x = title.width*0.5 + img.width + 6
title.y = 30
local subtitle = display.newText( row.subtitle, 0, 0, native.systemFont, 12 )
subtitle:setTextColor(80,80,80)
–subtitle:setTextColor(180,180,180)
g:insert(subtitle)
subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.y = title.y + title.height + 6
return g
end
}
end[/lua] [import]uid: 8970 topic_id: 4890 reply_id: 304890[/import]