Reputation: 157
I have a tableview which contains data fetched from sqlite. I am able to delete a record by setting editing to true.
I want to check some condition before deleting the row. If the condition is true, the row is deleted, however when the condition is false, I'm getting the following error:
no row found for index. in -[TiUITableViewProxy insertRowBefore:] (TiUITableViewProxy.m:500)
table.addEventListener('delete', function(e) {
if(some condition){
db.execute("DELETE FROM tbltest WHERE rowid ='" + e.source.rowid + "'");
} else {
alert('Deletion is not possible');
table.insertRowBefore(e.index, e.row);
}
}
Any ideas?
Upvotes: 0
Views: 1142
Reputation: 8692
table.addEventListener('delete', function(e) {
try{
db.execute("DELETE FROM tbltest WHERE rowid ='" + e.source.rowid + "'");
}
catch(e) {
alert('Deletion error' + e);
table.insertRowBefore(e.index, e.row);
}
}
Upvotes: 1
Reputation: 220
I have a feeling that table indexes are not refreshed. So when you're looking up a row under the old row index, it thinks there's nothing there.
Try this:
table.addEventListener('delete', function(e) {
table.setData(table.data);
if(some condition){
db.execute("DELETE FROM tbltest WHERE rowid ='" + e.source.rowid + "'");
} else {
alert('Deletion is not possible');
table.insertRowBefore(e.index, e.row);
table.setData(table.data);
}
}
Although I'm not sure you won't loose the e.row after you do setData. Another note, is that this won't work if you're deleting the last row. So check row's index, compare to the rows length, and if it's larger, just append the row. So the code becomes
table.addEventListener('delete', function(e) {
table.setData(table.data);
if(some condition){
db.execute("DELETE FROM tbltest WHERE rowid ='" + e.source.rowid + "'");
} else {
// below will only work if you have 1 or no explicit sections in the table if you have more, you'll need to iterate through them
if (e.index >= table.data[0].rows.length) {
table.appendRow(e.row);
} else {
table.insertRowBefore(e.index, e.row);
}
table.setData(table.data);
}
I haven't tested this, if it doesn't work, hopefully it will at least set you on the right track :)
Upvotes: 1