Problem
The number of rows are not printed correctly on the console after removing rows from the tableview.
Please run the sample code below to reproduce the issue. We add rows using addRows() method and remove using removeRows() method providing the number of rows to be added or removed as arguments. It looks like the table data is manipulated asynchronously.
The workaround is to put a time interval wait for the table data changes to take effect. (Uncomment the passedTest() method to implement this workaround).
*Note that you will see that the UI is updated, but the data[0].rows data structure is only updated after a delay*
Code Sample
{noformat}
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor:'#fff'
});
var table = Ti.UI.createTableView( {
backgroundColor:'white',
top: 0
});
win1.add(table);
win1.open();
//passedTest();
failedTest();
/**
* This test passes because we wait for the delete to complete before running the assertion
*/
function passedTest() {
Ti.API.info("Running passed test");
// add 1 row
addRows(5);
// should have 5 rows
assertRowCount(5);
// remove 1 row
removeRows(1);
// wait for the delete to complete - should have 4 rows
waitForRowCount(4);
Ti.API.info("Finishing passedHi Tim test");
}
/**
* This test fails because the delete is apparently asynchronous
*/
function failedTest() {
Ti.API.info("Running failed test");
// add 1 row
addRows(5);
// should have 5 rows
assertRowCount(5);
// remove 1 row
removeRows(1);
// should have 4 rows
assertRowCount(4);
Ti.API.info("Finishing failed test");
}
function waitForRowCount(expectedCount) {
var timer = setInterval(function () {
if (table.data[0].rows && table.data[0].rows.length == expectedCount) {
clearInterval(timer);
assertRowCount(expectedCount);
}
}, 100);
}
function assertRowCount(expectedCount) {
var actualCount = 0;
if (table.data[0].rows) {
actualCount = table.data[0].rows.length;
}
if (actualCount == expectedCount) {
Ti.API.info("Row count is correct - expected: "+expectedCount+" found: "+actualCount);
} else {
Ti.API.error("Row count is incorrect - expected: "+expectedCount+" found: "+actualCount);
}
}
function removeRows(count) {
if (!table.data[0] || table.data[0].rows.rowCount < count) {
return;
}
var tableData = table.data[0].rows;
for (i=0;i
Comments
JSON Source
Pull pending https://github.com/appcelerator/titanium_mobile/pull/2522
Pull pending https://github.com/appcelerator/titanium_mobile/pull/2545 against 2_1_X
Correct number of rows returned on adding and removing rows. Verified on: Titanium Studio: 2.1.1.201207161421 Titanium SDK: 2.1.1.v2012071618060 iOS: iPhone Simulator 5.0
Reopening issue to update the labels.
Anvil testcase PR https://github.com/appcelerator/titanium_mobile/pull/4940