delete tableViewRow from tableView
I'm having some trouble deleting a tableViewRow from my tableView, it deletes a row, but it's always the first row that gets deleted.
Here is a part of my code:
http://pastebin.com/jd6yy4ei
So for some reason, it always takes the first row.
3 Answers
-
Accepted Answer
I think you'd have a better chance of this working. I haven't tested it but it seems like it should work. Basically I am storing the current row in a field called myrow in the button itself and then in the click I am looking at the source and grabbing the myrow field that I created in the button. That also required adding the e variable in the function.
// rij is aanpasbaar if(editable == true) { var removeButton = Ti.UI.createButton({ width: '26', height: '25', backgroundImage: 'images/removeProduct.png', left: "226", myrow: row }); view.add(removeButton); row.addEventListener('click', function() {}); removeButton.addEventListener('click', function(e) { tableView.deleteRow(e.source.myrow); }); } else { }
-
On a slightly different note, and if your removing ALL rows from a table, I've found that removing rows in reverse order works. I had an issue where I knew I only had 3 rows but removing them with incrementing indexes 0,1,2 always failed on index 2 and left a row in the table.
Reversing the row removal works correctly, so I assume the index, although specifically set on the row when it's added, is automagically being reset as rows are removed.
Ti.API.info('Total Rows in table = '+resultview.data[0].rowCount); // remove existing rows for (var tr=2; tr>=0;tr--) { resultview.deleteRow(tr); Ti.API.info("removing row at "+tr); }
-
This post says the online documentation is wrong.
http://developer.appcelerator.com/question/35381/documentation-titaniumuitableviewdeleterow
The KitchenSink also demonstrates this behavior as well.
tableview.deleteRow(index,{animationStyle:Titanium.UI.iPhone.RowAnimationStyle.UP});
It seems you need store the index (0 for the first row, 1 for second row, etc) and then use that number to delete the row.