Titanium JIRA Archive
Appcelerator Community (AC)

[AC-1681] iOS: TableView insertRowAfter throws error on last index

GitHub Issuen/a
TypeBug
Priorityn/a
StatusResolved
ResolutionCannot Reproduce
Resolution Date2015-09-29T03:00:03.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsdefect, ios, iphone
ReporterDom Reilly
AssigneeShak Hossain
Created2015-02-26T16:56:54.000+0000
Updated2016-03-08T07:38:10.000+0000

Description

Hi, I've been having an issue with the code from https://gist.github.com/rileydutton/946647, which I have slightly modified, as shown below in the code. Basically the code works fine, when you click parent 2, however if you do the following: 1) on app load click parent 2 2) click parent 1 3) then click parent 2 a few times you are shown an error, stating: 'no row found for index' I have also noticed that if you dismiss the message, a new row called with the title 'child' and a number(comes from the sub collection) is present, and the error is no longer thrown while this is shown. Code: --------------------------------------------------------------------------------------------------
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

var win1 = Titanium.UI.createWindow({
	title : 'Tab 1',
	backgroundColor : '#fff'
});
var activeWindow = win1;
var layout = [{
	title : "Parent 1",
	_isParent : true,
	_isOpen : false,
	sub : [{
		title : "Child 1"
	}, {
		title : "Child 2"
	}]
}, {
	title : "Parent 2",
	_isParent : true,
	_isOpen : false,
	sub : [{
		title : "Child 3"
	}, {
		title : "Child 4"
	}]
}];
var tableView = Ti.UI.createTableView({
	style : Titanium.UI.iPhone.TableViewStyle.GROUPED,
	top : 0,
	height : Ti.Platform.displayCaps.platformHeight,
	data : layout
});

tableView.addEventListener("click", function(e) {

	//Is this a parent cell?
	if (e.row._isParent) {

		// check to see if the row is open
		if (e.row._isOpen) {
			for (var i = e.row.sub.length; i > 0; i = i - 1) {
				tableView.deleteRow(e.index + i);
			}
			e.row._isOpen = false;
		} else {
			// Add the children.
			var currentIndex = e.index;
			for (var i = 0; i < e.row.sub.length; i++) {
				tableView.insertRowAfter(currentIndex, e.row.sub[i]);
				currentIndex++;
			}
			e.row._isOpen = true;
		}

	}

});

activeWindow.add(tableView);
activeWindow.open();

Attachments

FileDateSize
Screen Shot 2015-02-26 at 16.45.14.png2015-02-26T16:56:54.000+000067834

Comments

  1. Papia Chowdhury 2015-08-21

    Hello, I have retested this issue with the latest environment and this is not a bug anymore. *Testing Environment:* Appcelerator Studio, build: 4.2.0.201508141038 Titanium SDK: 4.1.0 GA iOS simulator: iphone 5s(v8.1) OS X version: 10.9.5 *Test Case:* app.js
       var win = Titanium.UI.createWindow({
           backgroundColor : '#000'
       });
       var Data = [{
           title : "Parent 1",
       }, {
           title : 'Parent 2',
       }];
       var table = Ti.UI.createTableView({
           data : Data,
       });
       var value = true;
       var value1 = true;
       table.addEventListener('click', function(e) {
           switch(e.rowData.title) {
               case 'Parent 1':
                   if (value) {
                       var row = Ti.UI.createTableViewRow(); 
                       	var label1 = Ti.UI.createLabel({
                           text : 'Child 1',
                           color : 'red',
                           left : 45,
        
                       });
                       row.add(label1);
                       table.insertRowAfter(e.index, row);
                       value = false;
                       break;
                   } else {
                       table.deleteRow(e.index + 1);
                       value = true;
                       break;
                   }
               case 'Parent 2':
                   if (value1) {
                       var row = Ti.UI.createTableViewRow();
        
                       var label2 = Ti.UI.createLabel({
                           text : 'Child 2',
                           color : 'red',
                           left: 45
                       });
                       row.add(label2);
                     
                       table.insertRowAfter(e.index, row);
                       value1 = false;
                       break;
                   } else {
                       table.deleteRow(e.index + 1);
                       value1 = true;
                       break;
                   }
           }
       });
       win.add(table);
       win.open();
       
    Thanks.

JSON Source