Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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:

  1. Tap "Row 1" – icon clears

  2. Delete "Row 1"

  3. Tap "Row 2" and "Row 3" – no action

  4. 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:

  1. Delete "Row 1"

  2. Tap "Row 2" – "Row 3" updates WRONG

  3. Tap "Row 2" again – "Row 2" updates RIGHT

  4. Later taps ("Row 4") work too

— asked April 5th 2010 by Brad Broulik
  • delete
  • iphone
  • tableview
0 Comments

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.

    — answered February 25th 2011 by Shad Reynolds
    permalink
    1 Comment
    • This hack fixed a problem I was having with the 'click' event.index being off by 1 after deleting a row via the swipe editing function. Thanks!

      — commented August 2nd 2011 by Dave Macpherson
  • 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

    — answered January 16th 2011 by George Marmaridis
    permalink
    2 Comments
    • Thanks sir..!

      — commented May 2nd 2011 by sachin thakur
    • This hack worked for me also. My edit button had the tableView.editing = false; and I added the two lines of code above to fix the exception errors. Thanks for posting this Sachin.

      — commented June 17th 2011 by Sky Apperley
  • These were not fixed in the Ti 1.2.0 release.

    — answered April 6th 2010 by Brad Broulik
    permalink
    1 Comment
    • I desperately need a fix for this too.

      — commented May 29th 2010 by Daniel Zimmermann
  • 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.

    — answered May 22nd 2011 by freshteapot #
    permalink
    0 Comments
  • Version 2.1.1. The table click event index property is still incorrect after deleting a row.

    — answered August 22nd 2012 by Simon Buckingham
    permalink
    0 Comments
  • 3.1.3 still has this issue. Guys when you will fix it? Working with tables is main functionality in many applications!

    — answered October 9th 2013 by Andrew Kuzkov
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.