TableView delete (row) indexing issues
Context: email application with "unread" images in leftImage, with several labels added to row
Setting (row)rowData[e.index].leftImage = '' clears icon successfully while table is unchanged.
After a a row is deleted, the new row at that index and those that come after cannot be cleared. Those before that index STILL CAN. Should this work?
Here's a modified version of (Kitchen Sink) table_view_delete_2.js event handler which shows this behavior.
// create table view data object
var data = [
{title:'Row 1', leftImage: '../images/appcelerator_small.png', hasChild:true, foo:'row1'},
{title:'Row 2', leftImage: '../images/appcelerator_small.png', hasDetail:true, foo:'row2'},
{title:'Row 3', leftImage: '../images/appcelerator_small.png', foo:'row3'},
{title:'Row 4', leftImage: '../images/appcelerator_small.png', foo:'row4'}
];
...
// create table view event listener
tableview.addEventListener('click', function(e)
{
// event data
var index = e.index;
var section = e.section;
var row = e.row;
var rowdata = e.rowData;
rowdata.leftImage = '';
rowdata.className = 'selected'; // doesn't help
Titanium.UI.createAlertDialog({title:'Table View',message:'row ' + row + ' index ' + index + ' section ' + section + ' row data ' + rowdata}).show();
});
Test:
Tap "Row 1" – icon clears
Delete "Row 1"
Tap "Row 2" and "Row 3" – no action
Tap "Row 4" – CRASH
If the correct solution is to use updateRow(), the result is "better" but still not right. The first click event, returns the "previous" index value of the row clicked –not its current/correct value.
Here's the event handler change (same: table_view_delete_2.js) for this:
// create table view event listener
tableview.addEventListener('click', function(e)
{
// event data
var index = e.index;
var section = e.section;
var row = e.row;
var rowdata = e.rowData;
var tStr = 'e.index = ' + e.index;
tableview.updateRow(e.index, {
title: tStr,
hasCheck: true,
className: 'selected'
});
Titanium.UI.createAlertDialog({title:'Table View',message:'row ' + row + ' index ' + index + ' section ' + section + ' row data ' + rowdata}).show();
});
To test:
Delete "Row 1"
Tap "Row 2" – "Row 3" updates WRONG
Tap "Row 2" again – "Row 2" updates RIGHT
Later taps ("Row 4") work too
6 Answers
-
I was just able to fix a problem very similar to this by adding the following in the delete message.
tableView.addEventListener('delete', function(e) { tableView.setData(tableView.data); // <-- Hack. // do whatever else... });
Titanium 1.6 came out a few days and I do not know if this is fixed there or not.
-
I know my answer's propably not gonna help the original asker but… I think I've come up with a neat workaround!
when exiting delete mode, meaning right before
tableView.editing = false;
,
simply append and delete a blank row!code:
tableView.appendRow(Ti.UI.createTableViewRow()); tableView.deleteRow(tableView.data[0].rowCount-1); // or whatever the length of your table rows is tableView.editing = false;
The adding - removing isn't even visible to the user!
Titanium team… it's been 10 months since the original question… You couldn't just pull out an official fix? Come on…
Thank me later :P
-
These were not fixed in the Ti 1.2.0 release.
-
If you are having trouble. Getting a warning about "not being able to delete from the table".
It might be because you have misunderstood ( like me ) how the "delete" event works. It does the magic of deleting and animating the deletion for you.
You only have to take care of things like deleting the entry from the database, array, json object, xml etc.
-
Version 2.1.1. The table click event index property is still incorrect after deleting a row.
-
3.1.3 still has this issue. Guys when you will fix it? Working with tables is main functionality in many applications!