TableView:getRowAtIndex returns nil on valid rows

@Rob, I try to wait 5-10sec but still error. I use print command to get this…

I insert 3 row, and I delete first one, then I get this. but if I delete the last one is ok, same problem when I delete the second row.

Jan 17 23:41:56.272: Start Insert to Terpilih Table View

Jan 17 23:41:56.272: Row Index Peserta : 1

Jan 17 23:41:56.272: ID Peserta : 218

Jan 17 23:41:56.273: Number of Rows : 1

Jan 17 23:41:56.273: End Insert to Terpilih Table View 

Jan 17 23:41:57.211: Start Insert to Terpilih Table View

Jan 17 23:41:57.212: Row Index Peserta : 2

Jan 17 23:41:57.212: ID Peserta : 176

Jan 17 23:41:57.212: Number of Rows : 2

Jan 17 23:41:57.212: End Insert to Terpilih Table View 

Jan 17 23:41:58.713: Start Insert to Terpilih Table View

Jan 17 23:41:58.713: Row Index Peserta : 3

Jan 17 23:41:58.713: ID Peserta : 115

Jan 17 23:41:58.713: Number of Rows : 3

Jan 17 23:41:58.714: End Insert to Terpilih Table View 

Jan 17 23:42:01.926: Start Touch Delete Terpilih Row

Jan 17 23:42:01.927: Row Index : 1

Jan 17 23:42:01.927: Target Index : 1

Jan 17 23:42:01.927: Number of Before Delete Row : 3

Jan 17 23:42:01.927: Number of Before Delete Row : 2

Jan 17 23:42:01.927: End Touch Delete Terpilih Row

Note use object:getRowAtIndex()

Jan 17 23:42:05.769: Note : Nil. Row at index 1 shouldn’t be nil. Total Rows = 2

Jan 17 23:42:05.769: Start Print Data from Terpilih Table

Jan 17 23:42:05.770: [null,176]

Jan 17 23:42:05.770: End Print Data from Terpilih Table

Can you explain why you are doing this?  Can you post the actual code involved?  We probably don’t need to see whole thing, just the code involved with rendering/creating/deleteing table entries, etc.

What version of Corona SDK are you using?

Rob

this is the code…

local function renderTerpilihRow( event ) local phase = event.phase local row = event.row local params = event.row.params row.User = display.newText( row, params.name, 0, 0, "Helvetica Neue", 18 ) row.User:setFillColor( 1, 1, 1 ) row.User.anchorX = 0 row.User.x = 15 row.User.y = row.contentHeight / 5 row.ID = params.id end local function touchTerpilihRow( event ) local phase = event.phase local row = event.target if "release" == phase then terpilihLists:deleteRow( row.index ) end end -- Code execute when submit button clicked local terpilihTable = {} local totalRows = terpilihLists:getNumRows() for tp = 1, totalRows do if ( terpilihLists:getRowAtIndex( tp ) == nil ) then print( "Note : Null. Row at index " .. tp .. " shouldn't be null. Total Rows = " .. totalRows ) else terpilihTable[tp] = tonumber(terpilihLists:getRowAtIndex( tp ).ID) end end

I have that problem to, is that a bug or what?

@IronMan, if the tableView can display 10 total rows and your app has 20 total rows, then 10 of them are on screen, 10 of them are off screen.  If rows 1-10 are on screen, then tableView:getRowAtIndex( i ) will return nil for rows 11-20.

Rob

FYI in my experience the TableView widget will render one or two rows offscreen so they’re ready immediately during a fast scroll event. I haven’t ever measured exactly how many, but I’m pretty sure I’m right.

Otherwise tableView:getRowAtIndex() works as Rob describes.

@Rob, but if I delete the first row and call obj:getRowAtIndex() I get error cause data is nil, but if I delete the last row it’s fine.

so I use print to getNumRow of that table, and the reulst is different from the row that in the table. but if I delete one more then getNumRow get update.

but, I get idea to fix this problem now. but error for nil when row delete then getRowAtIndex() I think is a bug.

Are you saying that:

tableView:deleteRow(1)

tableView:getRowAtIndex(1)

is returning nil when the new row 1 should now be the previous row 2?

Rob

yes, is that a bug or should I use different approach?

<IronMan>: You should reference this topic re: deletion timing: http://forums.coronalabs.com/topic/47558-tableview-20-delete-a-row-without-animation/

Its likely because the code executes so fast, that it could be trying to reference the row right after it’s deleted but before the table has been re-indexed.  As @corona273 mentions, there are animations going on when deleting the row.  There could also be a micro timer in there that could give the row deletion a little async property and it purely could be a race condition.  Try dropping your get index call into a timer with say a 500ms delay and see if that changes the behavior.

@Rob, I try to wait 5-10sec but still error. I use print command to get this…

I insert 3 row, and I delete first one, then I get this. but if I delete the last one is ok, same problem when I delete the second row.

Jan 17 23:41:56.272: Start Insert to Terpilih Table View

Jan 17 23:41:56.272: Row Index Peserta : 1

Jan 17 23:41:56.272: ID Peserta : 218

Jan 17 23:41:56.273: Number of Rows : 1

Jan 17 23:41:56.273: End Insert to Terpilih Table View 

Jan 17 23:41:57.211: Start Insert to Terpilih Table View

Jan 17 23:41:57.212: Row Index Peserta : 2

Jan 17 23:41:57.212: ID Peserta : 176

Jan 17 23:41:57.212: Number of Rows : 2

Jan 17 23:41:57.212: End Insert to Terpilih Table View 

Jan 17 23:41:58.713: Start Insert to Terpilih Table View

Jan 17 23:41:58.713: Row Index Peserta : 3

Jan 17 23:41:58.713: ID Peserta : 115

Jan 17 23:41:58.713: Number of Rows : 3

Jan 17 23:41:58.714: End Insert to Terpilih Table View 

Jan 17 23:42:01.926: Start Touch Delete Terpilih Row

Jan 17 23:42:01.927: Row Index : 1

Jan 17 23:42:01.927: Target Index : 1

Jan 17 23:42:01.927: Number of Before Delete Row : 3

Jan 17 23:42:01.927: Number of Before Delete Row : 2

Jan 17 23:42:01.927: End Touch Delete Terpilih Row

Note use object:getRowAtIndex()

Jan 17 23:42:05.769: Note : Nil. Row at index 1 shouldn’t be nil. Total Rows = 2

Jan 17 23:42:05.769: Start Print Data from Terpilih Table

Jan 17 23:42:05.770: [null,176]

Jan 17 23:42:05.770: End Print Data from Terpilih Table

Can you explain why you are doing this?  Can you post the actual code involved?  We probably don’t need to see whole thing, just the code involved with rendering/creating/deleteing table entries, etc.

What version of Corona SDK are you using?

Rob

this is the code…

local function renderTerpilihRow( event ) local phase = event.phase local row = event.row local params = event.row.params row.User = display.newText( row, params.name, 0, 0, "Helvetica Neue", 18 ) row.User:setFillColor( 1, 1, 1 ) row.User.anchorX = 0 row.User.x = 15 row.User.y = row.contentHeight / 5 row.ID = params.id end local function touchTerpilihRow( event ) local phase = event.phase local row = event.target if "release" == phase then terpilihLists:deleteRow( row.index ) end end -- Code execute when submit button clicked local terpilihTable = {} local totalRows = terpilihLists:getNumRows() for tp = 1, totalRows do if ( terpilihLists:getRowAtIndex( tp ) == nil ) then print( "Note : Null. Row at index " .. tp .. " shouldn't be null. Total Rows = " .. totalRows ) else terpilihTable[tp] = tonumber(terpilihLists:getRowAtIndex( tp ).ID) end end