Titanium Community Questions & Answer Archive

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

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.

— asked November 1st 2010 by Wouter De Loose
  • tableview
  • tableviewrow
0 Comments

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
    {
    
    }
    
    — answered November 1st 2010 by John McKnight
    permalink
    4 Comments
    • Thanks for the answer, I just tried that, but it has no affect.

      It still deletes the first row.

      — commented November 1st 2010 by Wouter De Loose
    • Can you tell me more? Is this iOS? Android? What version of the mobile SDK?

      — commented November 1st 2010 by John McKnight
    • Hi there, I usually prefer to remove the item from the source data array (using array.splice) & rerun 'tableView.setData'

      — commented November 1st 2010 by Chris Reed
    • I'm testing this on iOS, Titanium SDK 1.4.1.1

      — commented November 1st 2010 by Wouter De Loose
  • 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);
    }
    
    — answered November 23rd 2010 by Martin Fisher
    permalink
    0 Comments
  • 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.

    — answered November 1st 2010 by John McKnight
    permalink
    7 Comments
    • I just tried it, and this kinda works, the only problem is that when I delete the row with index 3, another row has that index, but it doesn't have the same stored index. (if you understand me)

      — commented November 1st 2010 by Wouter De Loose
    • Yes I do understand. I am starting to think it may be better to maintain a separate array of "rows" and delete from the array and then refill the tableview from the array. If I think of a more appropriate solution, I will post it here.

      — commented November 1st 2010 by John McKnight
    • I think i'll try your solution with an array, it might come in handy because I'm also labeling the rows with numbers, so that way i can use the index in the array for the label.

      — commented November 1st 2010 by Wouter De Loose
    • I seem to run into a litle problem with this, when i output the tableView data, it returns me a tableViewSection

      [INFO] (
      "[object TiUITableViewSection]"
      )

      — commented November 1st 2010 by Wouter De Loose
    • Yes. The data property does not seem to return the rows as expected. What I do is maintain an array and after I make any changes to the array I rebuild the rows for the tableview.

      — commented November 1st 2010 by John McKnight
    • I understand, the only problem that I had whas that the table was placed in a scrollableView, because I could have seperate tables, so I got stuck because I couldn't store the array globaly.

      But what I did was created an array with arrays, and then use the currentPage from the scrollableView to select the right array.

      — commented November 1st 2010 by Wouter De Loose
    • var rows = tableViewArray[scrollView.currentPage - 1];
      rows.splice(rows.indexOf(row), 1);
      tableView.setData(rows);

      this turned out to work great, thanks for the help

      — commented November 1st 2010 by Wouter De Loose
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.