Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-9890] iOS: Adding and deleting tableview rows programmatically does not return correct number of rows

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2012-08-02T09:27:42.000+0000
Affected Version/sRelease 2.1.0
Fix Version/sSprint 2012-14 API, Release 2.1.1, Release 3.0.0
ComponentsiOS
LabelsSupportTeam, api, module_tableviewrow, qe-testadded
ReporterVarun Joshi
AssigneeVishal Duggal
Created2012-07-05T14:23:43.000+0000
Updated2013-12-10T05:54:15.000+0000

Description

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

  1. Vishal Duggal 2012-07-09

    Pull pending https://github.com/appcelerator/titanium_mobile/pull/2522
  2. Vishal Duggal 2012-07-12

    Pull pending https://github.com/appcelerator/titanium_mobile/pull/2545 against 2_1_X
  3. Satyam Sekhri 2012-07-16

    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
  4. Neha Chhabra 2012-08-02

    Reopening issue to update the labels.
  5. Shameer Jan 2013-12-10

    Anvil testcase PR https://github.com/appcelerator/titanium_mobile/pull/4940

JSON Source