Problem deleting rows in tableview

Hi guys, i’m using 1076 build, and i have a problem when i try to delete a row in a tableview. To know the number of rows of my tableview i’m using “#mytableview._view._rows”.
 
Every time I insert a new row in my table view, I show the total rows with the above command.
 
 [lua]mytableview:insertRow(…)
print(#mytableview._view._rows) --> 1
mytableview:insertRow(…)
print(#mytableview._view._rows) --> 2
mytableview:insertRow(…)
print(#mytableview._view._rows) --> 3
mytableview:insertRow(…)
print(#mytableview._view._rows) --> 4
mytableview:insertRow(…)
print(#mytableview._view._rows) --> 5[/lua]

However, if I delete a row that is not the last I get the following:

[lua]
mytableview:deleteRow(3)
print(#mytableview._view._rows) --> 5 (wrong value, correct is 4)
mytableview:deleteRow(2)
print(#mytableview._view._rows) --> 5 (wrong value, correct is 3)
[/lua]

If instead of delete row by row, i delete all then it works correctly

[lua]mytableview:deleteAllRows()
print(#mytableview._view._rows) --> 0 ok!!![/lua]

Please, I need your help, I think it has to be a bug of Corona

Thanks in advance

Hi good morning everyone, I put a small example of my code:

[lua]-- Import the widget library
local widget = require( “widget” )
local list

– Handle row rendering
local function onRowRender( event )
   local phase = event.phase
   local row = event.row

   local rowTitle = display.newText( row, "List item " … row.index, 0, 0, native.systemFontBold, 16 )
   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 touch events
local function onRowTouch( event )
   local phase = event.phase
   local row = event.target

   if “press” == phase then
      list:deleteRow(row.index)
      print("deleting rows: " … #list._view._rows)
   end
end

– Create a tableView
list = widget.newTableView
   {
   top = 38,
   width = 320,
   height = 448,
   maskFile = “mask-320x448.png”,
   onRowRender = onRowRender,
   onRowTouch = onRowTouch,
   }

– insert rows into list (tableView widget)
for i = 1, 10 do
   list:insertRow{
      height = 72,
   }
   print("adding rows: " … #list._view._rows)
end[/lua]

At the end of the code, when I insert rows, everything works correctly, the “print” shows the total number of rows correctly.

The problem is when I do the following:

1st. I touch the row # 3, and removed herself, but the “print” shows me: “deleting rows: 10” when it should be “deleting rows: 9”.

2nd. But worst of all is when I now try to delete the row # 2 and get the following error:

?:0: attempt to index field ‘?’ (a nil value)

stack tracebarck:

   [C]: ?

   ?: in function <?:794>

   (tail call): ?

   c:\users … \ main.lua:22: in function '_onRowTouch’

   ?: in function <?:365>

   ?: in function <?:218>

well then this is it, I hope you can help me, I’m starting to develop a business application and this is very important because in it, I will use several widgets tableview.

Thanks in advance

Hello, I’m new to Corona, I’m testing with new widgets to see if use in my applications and the tableview I have the same problem.

I hope someone can help with this.

Regards

First of all, a word of warning:
You are using private properties that are not meant to be used in your apps. They may change at any time and will then break your code.

At the moment there is no public property to get the current number of rows in a tableview. I’d recommend using your own variable to keep a row-count.

However if you still want to go down the path of using private properties you could use list._view.numChildren instead. Keep in mind that this updates only after the handler has finished.

You also have a few issues in your handlers:
In onRowRender there’s no event.phase. It should be event.name.
In onRowTouch you should use event.row to get the row.

I’d also recommend testing for the “release” event instead of “press”.

Hello ingemar, first of all thanks for answering.

I changed the use of private variables for my own variables, but before that, I tried using “list._view.numChildren” and did not work.

As you can see in the following code I have also taken into account the rest of the advice you have given me, but when I delete the third row working properly and when I delete the second row then gives me the same error at first.

[lua]-- Import the widget library
local widget = require( “widget” )
local list
local listRows = {} – my new table

– Handle row rendering
local function onRowRender( event )
   local row = event.row

   local rowTitle = display.newText( row, "List item " … row.index, 0, 0, native.systemFontBold, 16 )
   rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )
   rowTitle.y = row.contentHeight * 0.5
   rowTitle:setTextColor( 0, 0, 0 )

   table.insert(listRows, row.index) – Add the row.index to my table
end

– Handle row touch events
local function onRowTouch( event )
   local phase = event.phase
   local row = event.row
   local found

   if “release” == phase then
      found = table.indexOf(listRows, row.index)
      table.remove(listRows, found) – Delete the row from my table
      list:deleteRow(row.index)
   end
end

– Create a tableView
list = widget.newTableView
{
top = 38,
width = 320,
height = 448,
maskFile = “mask-320x448.png”,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}

– insert rows into list (tableView widget)
for i = 1, 10 do
   list:insertRow{
      height = 72,
   }
end
[/lua]

I think the problem is in the function “deleteRow” that have some kind of problem, since I’ve been looking at the tableview documentation and I think I’m using it correctly.

Thanks in advance

Hi,

I meant to just keep a simple count like this:

Note: I tested the code, and I don’t get an error when deleting the 2nd row after deleting the third. I can delete rows in any order without errors.

I’m on Corona SDK 2013.1093, iMac OSX 10.8.3

EDIT:

Beware that onRowRender also gets called every time a row is updated as well.

local widget = require( "widget" ) local list local listRowCount = 0 -- Handle row rendering local function onRowRender( event ) local row = event.row local rowTitle = display.newText( row, "List item " .. row.index, 0, 0, native.systemFontBold, 16 ) 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 touch events local function onRowTouch( event ) local phase = event.phase local row = event.row local found if "release" == phase then listRowCount = listRowCount - 1; list:deleteRow(row.index) print("number of rows ", listRowCount); end end -- Create a tableView list = widget.newTableView { top = 38, width = 320, height = 448, maskFile = "mask-320x448.png", onRowRender = onRowRender, onRowTouch = onRowTouch, } -- insert rows into list (tableView widget) for i = 1, 10 do list:insertRow{ height = 72, } listRowCount = listRowCount + 1 end

Hi, your idea is correct and the counter works well as I have done and how you tell me in your last post. But this does not mean that there is an error in “deleteRow” if you can test yourself this small example in the simulator.

First touch on row 3 to remove it and later, when you try to touch on to delete row 2 will also give you an error.

?:0: attempt to index field ‘?’ (a nil value)

stack tracebarck:

   [C]: ?

   ?: in function <?:794>

   (tail call): ?

   c:\users … \ main.lua:22: in function '_onRowTouch’

   ?: in function <?:365>

   ?: in function <?:218>

 

I hope your help

Hi, I’m on Corona SDK 2013.1076 (last public build), on Windows 7.

I would like someone to confirm that this problem is already solved.

thaNKS

Yeah, I saw that you were Starter, so I downloaded 1076 and tried with that version and got the same error.

It seems like there was a bug that was fixed in a later build, as 1093 works without giving any errors.

Many many thank you for your help Ingemar, will wait then to the next public build, but anyway I’m thinking of purchasing a pro license.

Thank you very much again for your time and help.

If you get the Pro licence now before the end of April, you’ll be able to continue to get the Pro licence for the next two years at the reduced price of $349  :). If you wait until May you’ll pay the full price of $599 every year.

ok, thanks for the information, I’ll think about it and take a decision before April 30.

regards

hello good morning, we have acquired the pro license and we are using build 1093.

Following the example of pjavelasco, i have proven to work correctly the “deleteRow” function. But when I delete a row, and after try to add a new row I get an error.

I have taken the example of “pjavelasco” and I inserted a button to add rows:

[lua]-- Import the widget library
local widget = require( “widget” )
local list
local addButton
local listRows = {} – my new table

– Handle row rendering
local function onRowRender( event )
   local row = event.row

   local rowTitle = display.newText( row, "List item " … row.index, 0, 0, native.systemFontBold, 16 )
   rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )
   rowTitle.y = row.contentHeight * 0.5
   rowTitle:setTextColor( 0, 0, 0 )

   table.insert(listRows, row.index) – Add the row.index to my table
end

– Handle row touch events
local function onRowTouch( event )
   local phase = event.phase
   local row = event.row
   local found

   if “release” == phase then
      found = table.indexOf(listRows, row.index)
      table.remove(listRows, found) – Delete the row from my table
      list:deleteRow(row.index)
   end
end

– Create a tableView
list = widget.newTableView
   {
   top = 38,
   width = 320, 
   height = 448,
   maskFile = “mask-320x448.png”,
   onRowRender = onRowRender,
   onRowTouch = onRowTouch,
   }

– insert rows into list (tableView widget)
for i = 1, 3 do
   list:insertRow{
      height = 72,
   }
end

local function addRow(event)
   if event.phase == “ended” then
      list:insertRow{
         height = 72,
         }
   end
end

– Create a button for insert new rows
addButton = widget.newButton{
   left = 25,
   top = 5,
   width = 100,
   height = 30,
   label = “Add”,
   onEvent = addRow,
}
[/lua]

In the code above, my modus operandi is as follows:

  1. I eliminate the third row by touch on herself

  2. Pulse the “add” button to insert a new row and I get the following error:

?:0: attempt to perform arithmetic on field ‘contentHeight’ (a nil value) 

stack traceback:

    [C]: ?

  ?:in function <?:812>

  (tail call): ?

  c:…\main.lua: 54 in function '_onEvent’

 …

I hope your help

thanks in advance

Looks like you’ve found a bug.

You can submit a report by using the “Report a bug” link at the top of the page.

I have found one inconvenient bug.

in my case i wanna delete one row from tableview.

while i scroll the list and click delete button ,  it failed and said “Warning: A row cannot be deleted whilst the tableView is scrolling”

But i was not scrolling the tableview! is it possible to delete and scroll at the same time?

However if i touch that tableview once and click delete button again, it success !!

that’s weird!

Owen

Hi good morning everyone, I put a small example of my code:

[lua]-- Import the widget library
local widget = require( “widget” )
local list

– Handle row rendering
local function onRowRender( event )
   local phase = event.phase
   local row = event.row

   local rowTitle = display.newText( row, "List item " … row.index, 0, 0, native.systemFontBold, 16 )
   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 touch events
local function onRowTouch( event )
   local phase = event.phase
   local row = event.target

   if “press” == phase then
      list:deleteRow(row.index)
      print("deleting rows: " … #list._view._rows)
   end
end

– Create a tableView
list = widget.newTableView
   {
   top = 38,
   width = 320,
   height = 448,
   maskFile = “mask-320x448.png”,
   onRowRender = onRowRender,
   onRowTouch = onRowTouch,
   }

– insert rows into list (tableView widget)
for i = 1, 10 do
   list:insertRow{
      height = 72,
   }
   print("adding rows: " … #list._view._rows)
end[/lua]

At the end of the code, when I insert rows, everything works correctly, the “print” shows the total number of rows correctly.

The problem is when I do the following:

1st. I touch the row # 3, and removed herself, but the “print” shows me: “deleting rows: 10” when it should be “deleting rows: 9”.

2nd. But worst of all is when I now try to delete the row # 2 and get the following error:

?:0: attempt to index field ‘?’ (a nil value)

stack tracebarck:

   [C]: ?

   ?: in function <?:794>

   (tail call): ?

   c:\users … \ main.lua:22: in function '_onRowTouch’

   ?: in function <?:365>

   ?: in function <?:218>

well then this is it, I hope you can help me, I’m starting to develop a business application and this is very important because in it, I will use several widgets tableview.

Thanks in advance

Hello, I’m new to Corona, I’m testing with new widgets to see if use in my applications and the tableview I have the same problem.

I hope someone can help with this.

Regards

First of all, a word of warning:
You are using private properties that are not meant to be used in your apps. They may change at any time and will then break your code.

At the moment there is no public property to get the current number of rows in a tableview. I’d recommend using your own variable to keep a row-count.

However if you still want to go down the path of using private properties you could use list._view.numChildren instead. Keep in mind that this updates only after the handler has finished.

You also have a few issues in your handlers:
In onRowRender there’s no event.phase. It should be event.name.
In onRowTouch you should use event.row to get the row.

I’d also recommend testing for the “release” event instead of “press”.

Hello ingemar, first of all thanks for answering.

I changed the use of private variables for my own variables, but before that, I tried using “list._view.numChildren” and did not work.

As you can see in the following code I have also taken into account the rest of the advice you have given me, but when I delete the third row working properly and when I delete the second row then gives me the same error at first.

[lua]-- Import the widget library
local widget = require( “widget” )
local list
local listRows = {} – my new table

– Handle row rendering
local function onRowRender( event )
   local row = event.row

   local rowTitle = display.newText( row, "List item " … row.index, 0, 0, native.systemFontBold, 16 )
   rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )
   rowTitle.y = row.contentHeight * 0.5
   rowTitle:setTextColor( 0, 0, 0 )

   table.insert(listRows, row.index) – Add the row.index to my table
end

– Handle row touch events
local function onRowTouch( event )
   local phase = event.phase
   local row = event.row
   local found

   if “release” == phase then
      found = table.indexOf(listRows, row.index)
      table.remove(listRows, found) – Delete the row from my table
      list:deleteRow(row.index)
   end
end

– Create a tableView
list = widget.newTableView
{
top = 38,
width = 320,
height = 448,
maskFile = “mask-320x448.png”,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}

– insert rows into list (tableView widget)
for i = 1, 10 do
   list:insertRow{
      height = 72,
   }
end
[/lua]

I think the problem is in the function “deleteRow” that have some kind of problem, since I’ve been looking at the tableview documentation and I think I’m using it correctly.

Thanks in advance

Hi,

I meant to just keep a simple count like this:

Note: I tested the code, and I don’t get an error when deleting the 2nd row after deleting the third. I can delete rows in any order without errors.

I’m on Corona SDK 2013.1093, iMac OSX 10.8.3

EDIT:

Beware that onRowRender also gets called every time a row is updated as well.

local widget = require( "widget" ) local list local listRowCount = 0 -- Handle row rendering local function onRowRender( event ) local row = event.row local rowTitle = display.newText( row, "List item " .. row.index, 0, 0, native.systemFontBold, 16 ) 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 touch events local function onRowTouch( event ) local phase = event.phase local row = event.row local found if "release" == phase then listRowCount = listRowCount - 1; list:deleteRow(row.index) print("number of rows ", listRowCount); end end -- Create a tableView list = widget.newTableView { top = 38, width = 320, height = 448, maskFile = "mask-320x448.png", onRowRender = onRowRender, onRowTouch = onRowTouch, } -- insert rows into list (tableView widget) for i = 1, 10 do list:insertRow{ height = 72, } listRowCount = listRowCount + 1 end